For example, the following isn't allowed, and I'm not sure why:
> let f () =
let f2 (a : byref<int>) =
()
let mutable a = 0
f2 &a;;
My guess would be that the byref could be a mutable reference to a stack variable, which could go out of scope if f2 decides to store it somewhere. Or is it something else?
The .NET type system does not allow byref types to be used as generic type arguments (e.g. you can't create a List<byref<int>>
). Since (first class) F# functions are actually instances of the FSharpFunc<_,_>
type, this means that F# functions also can't use byrefs in their domain or range.
The error seems to stem from the fact that you are declaring f2
as a nested function. If you extract it from beneath f
, it compiles:
let f2 (a : int byref) = ()
let f () =
let mutable a = 1
f2 &a
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With