On 10/28/17 04:38, Andrei Alexandrescu wrote:
Walter and I decided to kick-off project Elvis for adding the homonym
operator to D.

Razvan Nitu has already done a good part of the work:

https://github.com/dlang/dmd/pull/7242
https://github.com/dlang/dlang.org/pull/1917
https://github.com/dlang/dlang.org/pull/1918

What's needed is a precise DIP that motivates the feature properly and
provides a good proposal for it. I'm no fan of bureaucracy but we really
need to be pedantic about introducing language features. Walter argued
thusly in a PR, and I agree:

"I'm concerned that the elvis operator is not well understood, and we
shouldn't be designing it in the comments section here. A DIP needs to
be written. Things like operator precedence, side effects, type
resolution, comparison with the operator in other languages, grammar
changes, lvalues, how it would appear in the generated .di file if it
isn't its own operator, etc., should be addressed."

A lowering looks like the straightforward approach, of the kind:

expr1 ?: expr2

==>

(x => x ? x : expr2)(expr1)

Who wants to join Razvan in Project Elvis?


Thanks,

Andrei


C# has extensive experience with this operator and I think it would be wise to study the history of what they did and why the did it. NOTE: I understand that other languages have it, and there are variations on the theme, but C# has many similarities to D and extensive "in practice" idioms.

C# got the Elvis operator before it got the Null Conditional operator. In C# it only covers the case: a == null. The reason is that in practice most devs only use it like so: a != null ? a : b.

The funny thing is that it was almost never used.

Some years later the C# team introduces the Null-Conditional operator: ?. which allows you to write: obj1?.obj2?.prop3 ?? constant.

NOW people start using Null Coalescing all over the place.

I am all for the Elvis operator, however I have two reservations about it. The first is that I don't see much use for it without a null-conditional. The second is that the current proposed syntax ?: is MUCH to easily confused with ?.

This is not easy to read: obj1?.obj2?.prop3?:constant.

When designing syntax sugar, ergonomics are very important, otherwise people won't use it. Microsoft spent a LOT of time and treasure to learn these lessons for us. I see no reason to ignore them just because "we don't like Microsoft"

My proposal would be to copy what MSFT did, expect that I would I would introduce both operators at the same time.

Syntax as follows: obj1?.obj2?.prop3 ?? constant

In practice I don't see much use of the idiom outside of null's. The ONLY other thing that would work there is a boolean field and you might as well just return the boolean itself because the return values have to match types.

For example:
return obj1.bool ?? obj2 //Error: incorrect return type
return obj1 ?? obj2 // Pass: if same type

I cannot actually imagine a scenario outside of objects (including strings) where you could actually use it since the left-hand side MUST evaluate to a boolean.

Also, I am going to start repeating this mantra: Just because something CAN be done in the library, does not mean it SHOULD be done in the library.

Ergonomics matters. Yes, I understand that D is a powerful language, but Syntax Sugar has it's place in taking common idioms and standardizing them in the language itself (English is loaded with stuff like that) so that everyone can "speak the same language".

--
Adam Wilson
IRC: LightBender
import quiet.dlang.dev;

Reply via email to