Many of the APIs I use from F # are nullable. I like to turn them into Options. Is there a simple built-in way to do this? Here is one way I did this:
type Option<'A> with
static member ofNull (t:'T when 'T : equality) =
if t = null then None else Some t
Then I can use Option.ofNullas follows:
type XElement with
member x.El n = x.Element (XName.Get n) |> Option.ofNull
Is there something inline that already does this?
Based on Daniel's answer, equalitynot required. Instead, a restriction may be used null.
type Option<'A> with
static member ofNull (t:'T when 'T : null) =
if t = null then None else Some t
source
share