I've defined a custom operator in F# like this:
static member (&?) value defaultValue =
if value = null then
defaultValue
else
value
The operator is defined within a type and should be called
in the following scenario:
I'm retrieving information about the system processors using WMI.
Now i want to get the VoltageCaps property, but since that might be null, i want to assign a default value in that case.
let voltage = (m.["VoltageCaps"] &? (0 :> obj))
Visual Studio tells me "The type 'obj' does not support the operator '&?'"
Am i missing something here or is the operator itself faulty?
Your operator takes an arg of type
obj
so you should define it as a standalone function, not a type member.Now you can do this:
I changed it to pattern match
null
due to much better performance than generic equality.