Safe Navigation Operator “?.” for D2 ?

Marc Schütz" <schuetzm at gmx.net> Marc Schütz" <schuetzm at gmx.net>
Sat Mar 1 03:57:03 PST 2014


>> I sometimes come across situations like this:
>>
>> writeln_or_whatever(person ? person.name : "");
>>
>> (This is a case of "means something".)
>>
>> Using `person?.name` is a bit shorter and DRYer. But it would 
>> be more useful if I could specify the default value it 
>> returns. With an explicit `maybe` this would be possible:
>>
>> person.maybe("<n/a>").name;
>
> I am also on the opinion that chaining on nullable objects
> encourages sloppy code and should therefore be avoided. Because
> the null case should often be handled. Chaining skips handling
> the null case. Thus this syntaxic sugar is a bad idea.

The null case _is_ handled, because you're explicitly specifying 
"maybe()". I can agree that it might lead to problems if it 
happens automatically and you don't see what's going on, but that 
isn't the case here.


More information about the Digitalmars-d mailing list