Nullable or Optional? Or something else?

Steven Schveighoffer schveiguy at yahoo.com
Thu Sep 3 09:54:08 PDT 2009


On Wed, 02 Sep 2009 15:39:28 -0400, Andrei Alexandrescu  
<SeeWebsiteForEmail at erdani.org> wrote:

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


I just thought of something else.  If you use alias this, then what  
happens here?

Optional!int i = null;

int n = i;

If you are counting on i to implicitly cast to the int using alias this,  
then this probably won't work right (I would think it should throw or  
require an explicit cast).  I guess if you alias this to a function (is  
that possible yet?) which checks for null first, then you may have better  
behavior, but you still should probably require a cast.

-Steve



More information about the Digitalmars-d mailing list