Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Nullable Reference Types in F#

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#?

like image 672
user279185 Avatar asked Aug 26 '20 20:08

user279185


People also ask

Should I use nullable reference types?

Although using nullable reference types can introduce its own set of problems, I still think it's beneficial because it helps you find potential bugs and allows you to better express your intent in the code. For new projects, I would recommend you enable the feature and do your best to write code without warnings.

What is the point of nullable reference types?

Nullable reference types aren't new class types, but rather annotations on existing reference types. The compiler uses those annotations to help you find potential null reference errors in your code. There's no runtime difference between a non-nullable reference type and a nullable reference type.

Does F# have null?

The null keyword is a valid keyword in F#, and you have to use it when you are working with . NET Framework APIs or other APIs that are written in another . NET language.

Can we assign null to reference types?

A reference isn't supposed to be null. The compiler enforces rules that ensure it's safe to dereference these variables without first checking that it isn't null: The variable must be initialized to a non-null value. The variable can never be assigned the value null .


1 Answers

From https://www.infoq.com/news/2019/04/FSharp-Nulls/,

"F# currently supports several versions of nullability. First there are normal .NET reference types. Today there is no way to unequivocally inform the compiler if a specific reference type variable is nullable or not, so their use in F# is discouraged."

"The preferred alternative is Option. Also known as a “maybe” type, this is a type- safe way to express the concept of nullability. When used with idiomatic F# code, you can only read the value after checking to see if it is non-null (not “none” in F# parlance). This is usually done via pattern matching."

However, the desire that there would be a clean way to interop with these ? annotations is recognized by the F# team, and a proposal for nullable reference types can be found here. As of today, the proposal is "Approved in principle" but no prototype implementation yet exists.

like image 112
piggyBot Avatar answered Oct 19 '22 21:10

piggyBot