Skip to content

Instantly share code, notes, and snippets.

@SLAVONchick
Last active March 4, 2020 14:57
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 SLAVONchick/d5534332ac541640e74a2eb0a1c756cf to your computer and use it in GitHub Desktop.
Save SLAVONchick/d5534332ac541640e74a2eb0a1c756cf to your computer and use it in GitHub Desktop.
tryParse srtp function
open System
let inline tryParse s =
let mutable tmp = Unchecked.defaultof<_>
if ( ^a : (static member TryParse : string * byref< ^a> -> bool ) (s, &tmp) )
then Some tmp
else None
let (|Int|_|) s : int option = tryParse s
let (|Int64|_|) s : int64 option = tryParse s
let (|Int16|_|) s : int16 option = tryParse s
let (|Guid|_|) s : Guid option = tryParse s
let s = "F5C2A6F4-9578-43AA-9D12-B166BBB33737"
match s with
| Int i -> printfn "Int %i" i
| Int64 d -> printfn "Int64 %d" d
| Guid g -> printfn "Guid \"%A\"" g
| _ -> printfn "don't know what it is: %s" s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment