Nullable or Optional? Or something else?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Thu Sep 3 08:37:02 PDT 2009


Steven Schveighoffer wrote:
> On Thu, 03 Sep 2009 10:40:13 -0400, Leandro Lucarella <llucax at gmail.com> 
> wrote:
> 
>> Andrei Alexandrescu, el  2 de septiembre a las 14:39 me escribiste:
>>> I plan to add a Nullable struct to Phobos (akin to C#'s Nullable, 
>>> Boost's Optional).
>>>
>>> Apparently a good design is to define Optional!T with a minimum of 
>>> member functions (ideally none) and have it use the "alias this" 
>>> feature to masquerade as a
>>> T. That way Optional!T looks and feels much like a T, except that it 
>>> supports a function
>>>
>>> bool isNull(T)(Optional!T value);
>>>
>>> Am I on the right track? If so, what is the name you'd prefer for 
>>> this artifact?
>>
>> Maybe this is a very silly question, but what is exactly the difference
>> between Optional!T o/isNull(o) and T* o/o is null?
> 
> The difference is you don't have to store the T somewhere else.  That 
> is, an Optional!T contains both the value if it is not null AND whether 
> it is null or not.  With a T*, the value is stored elsewhere, and you 
> may have aliasing problems.
> 
> -Steve

Oh, that I forgot and is essential! A closer question is what's the 
difference between OptionalRef!T and T*. That difference is smaller 
because OptionalRef!T actually stores exactly a T* inside.

Andrei



More information about the Digitalmars-d mailing list