In F#, if you are interoperating with another .NET language, or if you using the AllowNullLiteral
attribute, a reference type can be null
. The example that comes to mind right away are strings:
let str: string = null
But with C# 8 and dotnet core 3, we can opt in to Nullable Reference Types. I would have to write the above code in C# like:
string? str = null;
Is there a way to also opt in to Nullable Reference Types in F# so that types defined in other languages cannot be null
, and if they could be, write them as a Nullable Reference like:
let str: string = null // error cannot do this
let str: string? = null
I'm aware that we can convert types that might be expected to be null
by using Options:
let str : string = null
let strOpt = Option.ofObj str
My question is: Is there away that makes it impossible to make a reference type null
, like string
, without explicitly declaring it to be nullable in F#?