Last active
March 4, 2020 14:57
-
-
Save SLAVONchick/d5534332ac541640e74a2eb0a1c756cf to your computer and use it in GitHub Desktop.
tryParse srtp function
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
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