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()