Nullable or Optional? Or something else?

bearophile bearophileHUGS at lycos.com
Wed Sep 2 15:30:11 PDT 2009


Andrei Alexandrescu:

> bool isNull(T)(Optional!T value);
> Am I on the right track? If so, what is the name you'd prefer for this 
> artifact?

It looks good and simple enough. The name may be "Nullable" or "Maybe" or something like that. This looks like a good use case of the alias this" feature.

Such Nullable() may contain a static if that avoids the presence of the internal boolean field if T is already a pointer or class reference, saving memory. And isNull() may work with pointer/class references too in input.

But such Nullable struct has to be designed thinking about the future too: I hope in future D2 will have nonull class references (a feature that's probably 5-10 times more useful than the Nullable wrapper), so you need a "nullable" or @nullable keyword or annotation to specify that a class reference can be null. In such situation it may be possible for the @nullable annotation to work on values too (in this case it wraps them into a Nullable struct). I don't know if such double usage of @nullable can lead to problems, I think not.

A possible future compiler optimization for arrays of Nullables is to split it into two arrays: an array of just the wrapped type, plus a bitvector to represent what items are null. But arrays of nullables probably will not be common enough to deserve such optimization...

Bye,
bearophile



More information about the Digitalmars-d mailing list