Safe Navigation Operator “?.” for D2 ?

Robert Clipsham robert at octarineparrot.com
Thu Feb 27 09:37:54 PST 2014


On Thursday, 27 February 2014 at 17:25:22 UTC, Chris Williams 
wrote:
>> just(myObject).method1().method2().method3()
>
> You can't do that. You're reducing your example code - which 
> was several dozen lines and only applied to objects for which 
> you had added the special handler code - to the end result. 
> After you've laid the framework for doing this, yes, you can do 
> it. But there's a bunch of work that has to go into it before 
> you get to that point. (Also, your implementation is far less 
> efficient than something which rewrites the code as a bunch of 
> nested "if (not null)" checks.)
>
> If your argument was that there are more important things for 
> the compiler team to work on, or that the syntax of the 
> language was already large enough without adding more things 
> for people to remember, then sure. But if we lived under the 
> premise that there's no reason to add features to a compiler 
> that abstract code down into a simpler syntax, then we'd have 
> never developed variables or functions.

Most of the code I posted would be hidden away in a library, and 
will work with any type with methods or UDFs. I'll admit it is 
incomplete (no implicit casting to the original return types for 
example), but it is possible. End user code would be exactly as 
that line is. I haven't checked the assembly, but given the 
simplicity of the generated code I'm fairly certain it will 
optimise to the same as the if/else chain (feel free to prove me 
wrong :)).

Robert


More information about the Digitalmars-d mailing list