This F# code is failing to compile:
let DoubleString (str : string) = str + str
let ExampleSetString (str : string byref) =
async {
str <- DoubleString str
return 1
}
I get the following error message on the line str <- DoubleString str:
The byref-typed variable 'str' is used in an invalid way. Byrefs cannot be captured by closures or passed to inner functions
I must take a byref variable, pass it into some pure functions within a computation expression and then set the value of this byref variable. How can I do this?
Dotnet restricts storing byref variables into fields, because byref can point at
&ar[42])&myObject.MyField)&myLocalVar)&(System.Runtime.CompilerServices.Unsafe.AsRef<myStruct> myVar))This leads to a lifetime problem: reference can outlive variable it's pointing at. It won't happen with case 1, but can happen with all other items. For example given function
FoocallingBarand passing value by reference will produce this stackFoohave calledBarand passed itaas reference&a.Barhave stored address ofainto variableb. This means thatbis byref-variable, updates on this variable will change value ina. When function completes it's work, memory is freed and can be reused by other function.Bazhave calledEggfunction, and now at memory where wasanow liesc. Attempting to change reference toanow can cause all sort of memory problems, fromAccessViolation(also known as Segmentation Fault in Linux), to data corruption. Worst thing that can happen when memory is corrupted, is for program to continue it's work.That's the reason why byref variables cannot be captured into closures, which are produced by computation expression.
Now let's return to actual problem - store string intro Azure blob. I'm not familiar with it, but I've found example for this, which can be adapted to following