Last active
July 23, 2017 09:05
-
-
Save giuliohome/104185116b28273403cb2b6ba6cec210 to your computer and use it in GitHub Desktop.
TrimNonEmptyString
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type TrimNonEmptyString internal (value : string) = | |
member __.Value = value | |
override __.ToString() = value | |
override __.Equals(yobj) = | |
match yobj with | |
| :? TrimNonEmptyString as y -> (__.Value = y.Value) | |
| _ -> false | |
override __.GetHashCode() = hash value | |
static member TryParse (value : string) = | |
if System.String.IsNullOrWhiteSpace value then | |
None | |
else | |
Some <| value.Trim() | |
with | |
interface System.IComparable with | |
member __.CompareTo yobj = | |
match yobj with | |
| :? TrimNonEmptyString as y -> | |
if __.Value > y.Value then 1 | |
elif __.Value < y.Value then -1 | |
else 0 | |
| _ -> invalidArg "TrimNonEmptyString" "cannot compare values of different types" | |
[<EntryPoint>] | |
let main argv = | |
System.Console.ReadLine() |> ignore; | |
//let x = TrimNonEmptyString (null) | |
printfn "*%A*" None | |
printfn "*%A*" null | |
printfn "*%A*" (TrimNonEmptyString (null)) | |
let test = TrimNonEmptyString.TryParse null | |
if not test.IsSome // mistyping | |
then printfn "*%s*" (Option.get test) | |
else printf "illegal state" | |
System.Console.ReadLine() |> ignore; | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment