difference between x = Nullable.init and x.nullify

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 4 01:51:44 PDT 2017


On Saturday, June 03, 2017 14:30:05 Kagamin via Digitalmars-d-learn wrote:
> On Saturday, 3 June 2017 at 06:19:29 UTC, Jonathan M Davis wrote:
> > Assigning Nullable!Test.init is equivalent to setting the
> > internal value to Test.init and setting _isNull to false.
>
> Eh? Does it mean Nullable is default initialized to some non-null
> default value?

Well, that depends on what you mean by null. Nullable!T doesn't use
pointers, so it can't be null like a pointer is null. The whole point of
Nullable!T is to emulate the null behavior of pointers while keeping
everything on the stack. It's a struct that contains two members:

T _value;
bool _isNull = true;

So, _value is default-initialized to T.init, and _isNull defaults to true.
Whether Nullable!T is "null" or not depends on the value of _isNull. So,
Nullable!T is default-initialized to null in the sense that _isNull is true,
and all of its member functions that check for "null" check whether _isNull
is true, so it's treated as "null" when it's default-initialized, but it's
not truly null in the sense that a pointer or class reference can be null.
If that's what you want, just create a T* rather than a Nullable!T.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list