I've been trying to learn F# lately and coming from object-oriented background, I have a little problems understanding generics in this one case.
Let's say I have the following functions:
let genericFunction<'a> (x: 'a) =
()
let myFunction (fn: ('a -> unit)) =
fn 2
fn 2UL
let test =
myFunction genericFunction
As a C# developer I'd expect fn
to be a generic function, which can take any argument. But the fn 2
call constraints the generic parameter 'a
to int
and thus it cannot be called with uint64
.
Why the generic type is constrained? How one would implement this without passing in two functions?
The compiler warns about the constraining and finally errors:
source_file.fs(5,8): warning FS0064: This construct causes code to be less generic than indicated by the type annotations. The type variable 'a has been constrained to be type 'int'.
source_file.fs(6,8): error FS0001: This expression was expected to have type
int
but here has type
uint64
Here's the error: http://rextester.com/RRRWTR28520