Skip to content

Instantly share code, notes, and snippets.

@giuliohome
Last active July 23, 2017 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giuliohome/104185116b28273403cb2b6ba6cec210 to your computer and use it in GitHub Desktop.
Save giuliohome/104185116b28273403cb2b6ba6cec210 to your computer and use it in GitHub Desktop.
TrimNonEmptyString
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