Proposal : allocations made easier with non nullable types.

Nick Sabalausky a at a.a
Mon Feb 9 23:58:29 PST 2009


"Daniel Keep" <daniel.keep.lists at gmail.com> wrote in message 
news:gmqmhk$2mb0$1 at digitalmars.com...
>
> How do you propose to write a template implementing nullable types when
> you aren't ALLOWED to use null?  You'd have to start casting around
> between integers and references, which is arguably worse than just
> supporting nullable types.
>

Devil's advocate:
---------------
struct Nullable(T)
{
   private T value; // Assuming "X x;" means "X x = new X();"
   private bool _isNull=true;

   void set(T val) {
       value = val;
       _isNull = false;
   }
   T get() {
       if (_isNull)
            throw NullableTypeException("value is null")
       else
            return value;
   }
   void makeNull() {
       _isNull = true;
   }
   bool isNull() {
       return _isNull;
   }
}
---------------

But admittedly, it's a kludge and vastly inferior to a nice well-implemented 
"X? x;" at the language level.





More information about the Digitalmars-d mailing list