My two cents

Adam D. Ruppe destructionator at gmail.com
Fri Oct 20 02:20:31 UTC 2017


On Friday, 20 October 2017 at 00:26:19 UTC, bauss wrote:
> return foo ? foo : null;
>
> where
>
> return foo ?? null; would be so much easier.

return getOr(foo, null);

That's really easy to do generically with a function. I wouldn't 
object to the ?? syntax, but if it really is something you write 
all over the place, you could just write the function.

> return foo ? foo.bar ? foo.bar.baz ? foo.bar.baz.something : 
> null;
>
> Which could just be:
>
> return foo?.bar?.baz?.something;

In dom.d, since I use this kind of thing somewhat frequently, I 
wrote a function called `optionSelector` which returns a wrapper 
type that is never null on the outside, but propagates null 
through the members. So you can do

foo.optionSelector("x").whatever.you.want.all.the.way.down

and it handles null automatically.


You can do that semi-generically too with a function if it is 
something you use really frequently.


More information about the Digitalmars-d mailing list