Created
October 3, 2019 14:04
-
-
Save atsapura/62644a098ef61cf6c5404e731383daed to your computer and use it in GitHub Desktop.
TryParse shortcut for Int.TryParse, Guid.TryParse etc.
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
let inline tryParse<'a when 'a: (static member TryParse: string * byref<'a> -> bool)> x = | |
let mutable res = Unchecked.defaultof<'a> | |
if (^a: (static member TryParse: string * byref<'a> -> bool) (x, &res)) | |
then Some res | |
else None | |
let a = tryParse<int>("19") | |
let b = tryParse<Guid>("4A9FA193-26D6-4D21-8610-02B060D43744") | |
let c = tryParse<DateTimeOffset>("ddd") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment