I think this is simple, but I can't get it.
I wish to create a new operator, e.g. (=??) , that can be used like '=' for property field bindings. That is the new operator will be used like:
MyProperty =?? NewValue
I would like the (=??) operator to do an Option.get NewValue so as to either return a value to MyProperty or immediately crash if NewValue has option none.
Can this be done in F# ??
Thank you for your consideration.
Addendum:
I still can't get this right. For example:
Assume a record called patient initialized as:
{Patient.RefId = Guid.NewGuid(); PatientAccountId = m.PatientAccountId; LastName = m.LastName; FirstName = m.FirstName; Mi = m.Mi;
BirthDate = m.BirthDate}
Now that Patient record defines Birthdate as a string, but m.Birthdate is string option, so the assignment:
BirthDate = m.BirthDate
fails.
But if I do BirthDate = Option.get m.BirthDate
all is good.
So how would I define an operator, e.g. =??, that does:
BirthDate = Option.get m.BirthDate`
as
BirthDate =?? m.BirthDate
TIA
Short Answer: No.
The <-
assignment operator is not something you can replace - operators are generally functions that compute values - there is no equivalent of C/C++ overloading of assignment operators with the ability to demand an argument be implicitly passed by reference etc. NOTE
So the best syntax you'll get is:
MyProperty <- NewValue.Value
Which is functionally identical to:
MyProperty <- Option.get NewValue
It's worth noting that if you're constructing a new object, you can also initialize properties as part of the creation, i.e.
let myThing = Thing(args)
myThing.MyProperty <- NewValue.Value
Can be collapsed to:
let myThing = Thing(args, MyProperty = NewValue)
NOTE there are actually various ways to have a reference passed to a function (look for the &
type signature symbol, including doing so implicitly IIRC). So it is actually technically possible to approximate it. However, I'd strongly recommend against doing so for normal usage, as it makes code really hard to scan to fish out where mutation is happening. The fact that mutable
, <-
, and Interlocked.Xyz(&thing, ...
) are the only places that mutation can happen is a major feature of the language.