Skip to content

Instantly share code, notes, and snippets.

@caindy
Last active December 6, 2016 01:53
Show Gist options
  • Save caindy/bca4f64f1212e2426d5e to your computer and use it in GitHub Desktop.
Save caindy/bca4f64f1212e2426d5e to your computer and use it in GitHub Desktop.
Using statically resolved types to remove a little boilerplate
// this works...
let n =
match System.Int32.TryParse("10") with
| (true,n) -> Some n | _ -> None
// but this is...
let inline tryParse (s : string) : ^o option =
let mutable o = Unchecked.defaultof<(^o)>
if (^o : (static member TryParse: string * ^o byref -> bool) (s, &o)) then Some o else None
// delicious
tryParse "10" |> Option.map ((+)10uy)
tryParse "10" |> Option.map ((+)10us)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment