Developing a plan for D2.0: Getting everything on the table

Jarrett Billingsley jarrett.billingsley at gmail.com
Wed Jul 15 14:42:22 PDT 2009


On Wed, Jul 15, 2009 at 5:37 PM, bearophile<bearophileHUGS at lycos.com> wrote:
> Daniel Keep:
>> Because:
>>
>> > struct NonNullable(T)
>> > {
>> >   T ptr;
>> >   alias ptr this;
>> >
>> >   this(T ptr)
>> >   {
>> >     assert(ptr !is null);
>> >     this.ptr = ptr;
>> >   }
>> >   this(NonNullable!T ptr)
>> >   {
>> >     this.ptr = ptr.ptr;
>> >   }
>> > }
>> >
>> > // Boom!  I just broke your non-nullable type!
>> > NonNullable!Object o;
>
> I am sorry, but I don't understand still. That's valid D2 code, and I can understand it. But from its look it seems (beside the not handy syntax, it's not as adding a "?") the opposite of what I was saying (that by default all D objects have to be non-nullable). Can you please help me understand? :-)

If you declare:

NonNullable! Object o;

o.ptr is initialized to null, because you aren't required to call its
constructor.  If you were able to disable the default constructor,
this line would give you an error, and you would be required to write:

NonNullable! Object o = new Object();

or so.



More information about the Digitalmars-d mailing list