Why can't the first class function type contain byrefs?

For example, the following is not valid, and I'm not sure why:

> let f () = let f2 (a : byref<int>) = () let mutable a = 0 f2 &a;; 

My assumption is that byref might be a volatile reference to a stack variable that might go out of scope if f2 decides to save it somewhere. Or is it something else?

+6
source share
2 answers

A .NET type system does not allow the use of byref types for use as arguments of a general type (for example, you cannot create a List<byref<int>> ). Since the (first class) F # functions are actually instances of type FSharpFunc<_,_> , this means that F # functions also cannot use byrefs in their area or range.

+9
source

The error seems to be related to the fact that you declare f2 as a nested function. If you extract it from f , it compiles:

 let f2 (a : int byref) = () let f () = let mutable a = 1 f2 &a 
0
source

All Articles