For the sample program:
type public MyClass(reasonForLiving:string) =
member x.ReasonForLiving with get() = reasonForLiving
let classFactory () = MyClas
The problem with the AllowNullLiteral
attribute is that in addition to allowing you to compare your objects to null, it also makes it possible to set your objects to null.
Assuming that this is not desirable for your use-case, there is an easy alternative with unobservable performance impact:
let inline isNull (x:^T when ^T : not struct) = obj.ReferenceEquals (x, null)
Then rather than doing if instance = null then
, do if isNull instance then
instead.
This will work for any reference type (including records and DUs), but does not introduce the possibility of setting objects of your F# types to null from F# – the best of both worlds.