Function with type contraints

Category:
Defining Types and Functions
Description: Define function with type contraints
Code:
let inline tryParseFunction2 s =
let mutable x = Unchecked.defaultof< ^T>
if (^T: (static member TryParse: string * ^T byref -> bool) (s, &x))
then Some x
else None

let TypeConstraintsSample3() =
let ``seq`` = [ "1"; "2"; "3"; "NotInt" ] |> List.map tryParseFunction2
``seq`` |> Seq.iter (printfn "%A")

Execution Result:
Some 1
Some 2
Some 3


Last edited Sep 14, 2011 at 2:27 AM by ttliu2000, version 1

Comments

No comments yet.