Project Elvis

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Nov 6 10:12:11 UTC 2017


On Monday, November 06, 2017 09:26:24 Satoshi via Digitalmars-d wrote:
> Look, this operator does not break anything. If you don't want to
> use it, just don't, but why do you force everyone else to not to
> use it, just because it is not adding anything "more valuable"
> than just better syntax?

_Everything_ that is added to the language complicates it further. It's one
more thing that everyone learning the language has to learn and know and
potentially deal with in code. Obviously, some things are worth the extra
complication, or we'd all be programming in C, but there is always a cost to
adding something, and the feature needs to be worth that cost.

Granted, the elvis operator as proposed is not all that complicated, but
adding it does make the language that much more complex, and it really
doesn't do much. All it does is take the expression

x ? x : y

and make it

x ?: y

It saves 2 characters plus the length of the variable name. That's it. And
it's optimizing an idiom that isn't going to tend to show up much in
idiomatic D code. It seems to be targeted primarily at code that does a lot
with classes and is written in such a way that it's not clear whether a
class reference should be null or not, whereas most D code doesn't do much
with classes. Rather, it does a lot with ranges and structs on the stack.
Obviously, some code uses classes, and I expect that some code would benefit
from the elvis operator, but I dispute that it's a common enough idiom to
merit being added the language.

Personally, while I frequently use the ternary operator, I almost never end
up with the left and middle branches being the same, which is the only place
that the elvis operator would be useful. And I don't think that Phobos does
it much either. So, unless a lot of D code out there is using a drastically
different coding style that does a lot with null, the elvis operator will
not help much D code.

So, IMHO, the elvis operator adds very little value, and I'd just as soon
not see the language complicated further without adding real value in the
process. So, I'll argue against it, but ultimately, it's not my decision.
Rather, it's up to Walter and Andrei, and they'll decide what they decide.

But don't expect anyone not to be unhappy about a feature being added to the
language when they don't think that the feature adds value, because there is
always a cost to a new feature. The difference is that you think that the
feature is worth the cost, not that the folks who don't want to use the
feature don't have to pay the cost.

- Jonathan M Davis



More information about the Digitalmars-d mailing list