Handling Null Values in F#

For some reason (I haven't yet investigated why) not (obj.ReferenceEquals(value, null)) performs much better than value <> null. I write a lot of F# code that is used from C#, so I keep an "interop" module around to ease dealing with null. Also, if you'd rather have your "normal" case first when pattern matching, you can use an active pattern:

let (|NotNull|_|) value = 
  if obj.ReferenceEquals(value, null) then None 
  else Some()

match value with
| NotNull ->
  //do something with value
| _ -> nullArg "value"

If you want a simple if statement, this works too:

let inline notNull value = not (obj.ReferenceEquals(value, null))

if notNull value then
  //do something with value

UPDATE

Here are some benchmarks and additional information on the performance discrepancy:

let inline isNull value = (value = null)
let inline isNullFast value = obj.ReferenceEquals(value, null)
let items = List.init 10000000 (fun _ -> null:obj)
let test f = items |> Seq.forall f |> printfn "%b"

#time "on"
test isNull     //Real: 00:00:01.512, CPU: 00:00:01.513, GC gen0: 0, gen1: 0, gen2: 0
test isNullFast //Real: 00:00:00.195, CPU: 00:00:00.202, GC gen0: 0, gen1: 0, gen2: 0

A speed-up of 775% -- not too bad. After looking at the code in .NET Reflector: ReferenceEquals is a native/unmanaged function. The = operator calls HashCompare.GenericEqualityIntrinsic<'T>, ultimately ending up at the internal function GenericEqualityObj. In Reflector, this beauty decompiles to 122 lines of C#. Obviously, equality is a complicated issue. For null-checking a simple reference comparison is enough, so you can avoid the cost of subtler equality semantics.

UPDATE 2

Pattern matching also avoids the overhead of the equality operator. The following function performs similarly to ReferenceEquals, but only works with types defined outside F# or decorated with [<AllowNullLiteral>].

let inline isNullMatch value = match value with null -> true | _ -> false

test isNullMatch //Real: 00:00:00.205, CPU: 00:00:00.202, GC gen0: 0, gen1: 0, gen2: 0

UPDATE 3

As noted in Maslow's comment, an isNull operator was added in F# 4.0. It's defined the same as isNullMatch above, and therefore performs optimally.


If you have a type that has been declared in C# or a .NET library in general (not in F#) then null is a proper value of that type and you can easily compare the value against null as posted by kvb. For example, assume that C# caller gives you an instance of Random:

let foo (arg:System.Random) =
  if arg <> null then 
    // do something

Things become more tricky if the C# caller gives you a type that was declared in F#. Types declared in F# do not have null as a value and F# compiler will not allow you to assign them null or to check them against null. The problem is that C# doesn't do this check and a C# caller could still give you null. For example:

type MyType(n:int) =
  member x.Number = n

In that case, you need either boxing or Unchecked.defaultOf<_>:

let foo (m:MyType) =
  if (box m) <> null then
    // do something

let foo (m:MyType) =
  if m <> Unchecked.defaultOf<_> then
    // do something

If you don't want to do anything in the null case, then you can use the unit value ():

match value with
| null -> ()
| _ -> // your code here

Of course, you could also do the null check just like in C#, which is probably clearer in this case:

if value <> null then
    // your code here

Tags:

C#

Interop

Null

F#