Skip to content

Instantly share code, notes, and snippets.

@atsapura
Created October 3, 2019 14:04
Show Gist options
  • Save atsapura/62644a098ef61cf6c5404e731383daed to your computer and use it in GitHub Desktop.
Save atsapura/62644a098ef61cf6c5404e731383daed to your computer and use it in GitHub Desktop.
TryParse shortcut for Int.TryParse, Guid.TryParse etc.
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