Disallow null references in safe code?

Adam D. Ruppe destructionator at gmail.com
Tue Feb 4 19:04:18 PST 2014


On Tuesday, 4 February 2014 at 18:26:10 UTC, deadalnix wrote:
> static if(isReferenceType!T) {
>     union {
>         T t;
>         typeof(null) __;
>     }
> }

cool, that would work. @nullable is now totally dead to me.

>> * Consistency with all other types. Nullable!int works, 
>> Nullable!Object can be passed to a template, inspected, etc. 
>> without new traits for isNullable and everything.
>>
>
> I'm not sure I understand that.

"@nullable int" wouldn't work. A nullable int needs a separate 
field to store if it has a value or not, since int == 0 is a 
valid payload.

A Nullable!T template can store the separate field if needed (use 
static if to add the field or use the union with typeof(null)) 
and thus work for all types with uniform user-side API.

> I'm really confused now. What are you defending ??

Built-in references become not-null by default. Library type 
Nullable!T is used when yo need null.


More information about the Digitalmars-d mailing list