In F# mantra there seems to be a visceral avoidance of null
, Nullable<T>
and its ilk. In exchange, we are supposed to instead use option types. To be honest, I don't really see the difference.
My understanding of the F# option type is that it allows you to specify a type which can contain any of its normal values, or None
. For example, an Option<int>
allows all of the values that an int
can have, in addition to None
.
My understanding of the C# nullable types is that it allows you to specify a type which can contain any of its normal values, or null
. For example, a Nullable<int>
a.k.a int?
allows all of the values that an int
can have, in addition to null
.
What's the difference? Do some vocabulary replacement with Nullable
and Option
, null
and None
, and you basically have the same thing. What's all the fuss over null
about?
This tells the compiler/runtime that MyProperty is nullable, and by default, will have a null value if no value is given. However in Typescript, the question mark operator is *not* a nullable operator, it instead denotes whether something is “optional”.
The Nullable type allows you to assign a null value to a variable. Nullable types introduced in C#2.0 can only work with Value Type, not with Reference Type. The nullable types for Reference Type is introduced later in C# 8.0 in 2019 so that we can explicitly define if a reference type can or can not hold a null value.
You typically use a nullable value type when you need to represent the undefined value of an underlying value type. For example, a Boolean, or bool , variable can only be either true or false .
Nullable variables may either contain a valid value or they may not — in the latter case they are considered to be nil . Non-nullable variables must always contain a value and cannot be nil . In Oxygene (as in C# and Java), the default nullability of a variable is determined by its type.
F# options are general, you can create Option<'T>
for any type 'T
.
Nullable<T>
is a terrifically weird type; you can only apply it to structs, and though the Nullable
type is itself a struct, it cannot be applied to itself. So you cannot create Nullable<Nullable<int>>
, whereas you can create Option<Option<int>>
. They had to do some framework magic to make that work for Nullable
. In any case, this means that for Nullables, you have to know a priori if the type is a class or a struct, and if it's a class, you need to just use null rather than Nullable. It's an ugly leaky abstraction; it's main value seems to be with database interop, as I guess it's common to have `int, or no value' objects to deal with in database domains.
Im my opinion, the .Net framework is just an ugly mess when it comes to null and Nullable
. You can argue either that F# 'adds to the mess' by having Option
, or that it rescues you from the mess by suggesting that you avoid just null/Nullable
(except when absolutely necessary for interop) and focus on clean solutions with Option
s. You can find people with both opinions.
You may also want to see
Best explanation for languages without null
Because every .NET reference type can have this extra, meaningless value—whether or not it ever is null, the possibility exists and you must check for it—and because Nullable
uses null
as its representation of "nothing," I think it makes a lot of sense to eliminate all that weirdness (which F# does) and require the possibility of "nothing" to be explicit. Option<_>
does that.
What's the difference?
F# lets you choose whether or not you want your type to be an option
type and, when you do, encourages you to check for None
and makes the presence or absence of None
explicit in the type.
C# forces every reference type to allow null
and does not encourage you to check for null
.
So it is merely a difference in defaults.
Do some vocabulary replacement with Nullable and Option, null and None, and you basically have the same thing. What's all the fuss over null about?
As languages like SML, OCaml and Haskell have shown, removing null
removes a lot of run-time errors from real code. To the extent that the original creator of null
even describes it as his "billion dollar mistake".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With