Last active
December 6, 2016 01:53
-
-
Save caindy/bca4f64f1212e2426d5e to your computer and use it in GitHub Desktop.
Using statically resolved types to remove a little boilerplate
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
// 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