After some reproduction of the membership restriction function F # and recording such a function:
let inline parse< ^a when ^a : (static member Parse: string -> ^a) > s = (^a: (static member Parse: string -> ^a) s)
This works great:
let xs = [ "123"; "456"; "999" ] |> List.map parse<int>
I am trying to write another func tryParse that uses the static tryParse method and transfers the result of the parsing to the 'a option type for better support in F #. Something like this does not compile:
let inline tryParse s = let mutable x = Unchecked.defaultof< ^a> if (^a: (static member TryParse: string * ^a byref -> bool) (s, &x)) then Some x else None
Mistake:
error FS0001: this expression was the expected type byref <'a> , but there is a type ' a ref
F # ref cells do not work either:
let inline tryParse s = let x = ref Unchecked.defaultof< ^a> if (^a: (static member TryParse: string * ^a byref -> bool) (s, x)) then Some x else None
What am I doing wrong?
generics f # constraints ref
ControlFlow Jan 11 '11 at 11:00 2011-01-11 11:00
source share