I need to assign to a byref parameter, but, using F# 4.0 and .NET 4.5.2 on a Windows 8x64 box, I keep getting complaints that This value is not mutable.
I can't change the signature, because I'm implementing a COM interface. Minimal broken example:
module Utils =
let bugFix1([<System.Runtime.InteropServices.Out>] ID : System.String byref) = ID <- "Hi!"
let bugFix1([<System.Runtime.InteropServices.Out>] ID : int byref) = ID <- 0
let bugFix1([<System.Runtime.InteropServices.Out>] ID : System.Guid byref) = ID <- System.Guid.NewGuid()
By this value
, it definitely means ID
, because it doesn't matter what I assign to ID
. Note too, that it doesn't matter whether the type is blittable or not, or whether it is heap- or stack-allocated.
Is there some way do declare ID
as mutable
?
I think you have discovered another bug (or undocumented feature?). This happens simply because your parameter name is capitalized. Surprise! :-)
These variants will work (omitted [<Out>]
for brevity):
let bugFix1(id : string byref) = id <- "Hi!"
let bugFix1(iD : string byref) = iD <- "Hi!"
But these will fail:
let bugFix1(Id : string byref) = Id <- "Hi!"
let bugFix1(ID : string byref) = ID <- "Hi!"
I have absolutely no idea why capitalization would matter. I would guess that this never arose before, because parameters always start with a lower-case letter by convention.
I intend to google some more and then file an issue.