Question

I was wondering if you can add something like a "null-safe"-operator in F#. I know that there might be plans for such an operator in C# for the next bigger release.

If there is no way to achieve such a behaviour, is there a way to wrap a statement that possibly could throw a NullReferenceException into a block that catches the exception and just returns null.

I thought of something like:

let sth = %% <@ someobject.method().another().andAnother() @>

where %% is a custom operator that executes the expression and checks for the exception.

One Attempt I made was:

open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Linq.QuotationEvaluation

let (~%%) (right:Expr) =
  try 
    right.CompileUntyped()
    right()
  with
  | ex -> null

But this does not do what I wanted (in fact it doesn't even compile :)

I read through:

Does anyone have ideas how to elegantly create such a one-line try-catch for chained methods?

Was it helpful?

Solution

I do not think you can implement something like the .? operator proposed for C# in a practically useful and syntactically convenient way for F# as a library feature. Although, I think it would be a very useful language extension, so I'd submit it to the F# user voice and perhaps submit a pull request :-).

Exception handling. Your code sample is not quite doing the same as C#, because you are just handling arbitrary exceptions. The point of the C# feature is that it inserts null checks after each . - you could do that by transforming the quotation and then compiling it, but the compilation is going to be slow. Your function could really just be:

let orNull f = try f () with :? NullReferenceException -> null

And then you can write

orNull <| fun () -> x.Foo().Bar()

.. but as mentioned earlier, this is just wrapping code in a standard try block and it handles exceptions, so it is not going to be doing the same thing as the C# code.

Computation builder. If you wanted a fancier F# solution, you could come up with a computation expression builder that lets you write something like this:

safe { let! a = x.Foo()
       let! b = a.Bar()
       return b }

This can do the null checks in the same way as the C# .? operator, but you need a separate binding for each part of the method call chain, so it is a bit more typing. The computation builder inserts a hidden null check in the Bind member like this:

type NullBuilder() = 
  member x.Return(v) = v
  member x.Bind(v, f) = if (box v) = null then null else f v 

let safe = NullBuilder()

OTHER TIPS

This is an interesting question. Fortunately, code that lives solely within the F# world (i.e. doesn't use .NET libraries authored in other languages) doesn't have the widespread problem that .? is intended to solve because nullability must be explicit (using [<AllowNullLiteral>]). However, there is a functional counterpart to this concept: the option type and bind function (bind is the foundation for workflows, as demonstrated by Tomas).

You could take something like this:

type T() =
    member this.M(b) = if b then Some this else None

T().M(true)
|> Option.bind (fun t -> t.M(true))
|> Option.bind (fun t -> t.M(false)) 
|> Option.bind (fun t -> t.M(true))

and define a bind operator

let (?>) o f = Option.bind f o

and shorten it to this:

T().M(true)
?> fun t -> t.M(true)
?> fun t -> t.M(false)
?> fun t -> t.M(true)

but, there's very little value in doing so. It's neither more concise nor clearer. However, I think it demonstrates:

  • the general concept you're dealing with
  • that it isn't inherently tied to null
  • how this is typically dealt with in a functional language

Maybe use F# worflows, extending the idea of the Maybe monad, and be able to write this:

let sth = maybe { someobject.method().another().another() }

See http://santialbo.com/blog/2013/03/27/monads-in-f-sharp/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top