In my code I'm passing around some structures by reference, declaring them mutable and using the &
sym开发者_如何学Gobol. The problem is that in some place the fields are corrupted (happens only in release mode) and I don't know absolutely why.
I have found a fix, using ref keyword instead of the address-of operator. I understand that you can interchange them freely (in case of instance member parameters) but why did it fix my issue?
Here's a small code sample illustrating this:
[<Struct>]
type MyStruct =
val mutable private i : int
val mutable private f : float
new (a, b) = { i = a; f = b }
member t.I = t.i
member t.F = t.f
type Printer () =
member t.Print(data : MyStruct byref) = printfn "%d %f" data.I data.F
let bar (p : Printer) =
let mutable x = new MyStruct(2, 8.0)
p.Print(&x)
let foo (p : Printer) =
let mutable y = new MyStruct(2, 8.0)
p.Print(ref y) // What is exactly the difference, under the hood?
let main () =
foo (new Printer())
bar (new Printer())
do main ()
Passing structures with byref seems useful only for interop scenarios or if you want to mutate structure's fields. This is not my case however. Should I consider passing structure types by value instead (about 20 bytes or so)?
Thanks!
The use of ref
in your example may not be exactly what you wanted to write:
let modify (data:int byref) = data <- 10
let foo() =
let mutable n = 15 // Note: Compiles without 'mutable'
modify (ref n)
printfn "%d" n // Prints '10'!!
You probably wanted something like this:
let foo() =
let n = ref 15
modify n
printfn "%d" (!n) // Prints '15'
So, what is the difference? ref
is a function that takes a value and creates a heap allocated reference cell. In the second example, the type of n
is ref<int>
, which is the reference cell. F# allows you to pass reference cells as arguments to byref
parameters - in which case, it creates a pointer to a field of the heap-allocated (reference cell) object.
In the second example, we create reference cell, pass a pointer to the reference cell to the modify
function and then use the !<ref>
syntax to get value from the reference cell. In the first example, we create a new reference cell when calling the modify
function (and the value of n
is copied from the stack to the heap allocated cell). The cell is not used after the call (and the value of n
stays 15)
On the other hand, a mutable
variable is simply stored on the stack and can be mutated. The main difference is that ref
is always heap allocated - using mutable
could be (in principle) a bit faster, because called function taking byref
directly modifies value on the stack.
精彩评论