Project Elvis

Jonathan M Davis newsgroup.d at jmdavisprog.com
Thu Nov 2 17:48:12 UTC 2017


On Thursday, November 02, 2017 09:46:06 H. S. Teoh via Digitalmars-d wrote:
> On Thu, Nov 02, 2017 at 12:50:47PM +0000, Nick Treleaven via Digitalmars-d
> wrote: [..]
>
> > I'd like to mention null-coalescing assignment syntax. Perl has `$a
> > //= $b`, and PHP has voted to support `$a ??= $b`, expanding to `$a =
> > $a ?? $b`.
>
> [...]
>
> > I expect D could do the same with `a ?:= b` or use the `??` operator
> > syntax.
> Isn't the `??` syntax what the Elvis operator `?:` supposed to do?
>
> Given that the proposed Elvis operator would be `?:`, I'd expect the
> corresponding assignment operator would be `?:=`, in following the
> pattern of the other op-assign operators.

In a previous thread, it was stated that in other languages (no idea which
ones), ?? tests specifically for null, whereas ?: tests for true.

Other, related operators were discussed as well - including ?. - which if I
understood correctly, allows you to call a member function only if the
object isn't null.

But going into other operators like that starts taking this well beyond the
basic idea of shortening the ternary operator for the x ? x : y case like
the elvis operator does, and for better or worse, that takes the DIP in a
whole new direction. However, if we do add ?:, I think that it's pretty
clear that some folks will be asking for stuff like ?. or ?:=, since I think
that it's largely the case that the folks who want the elvis operator are
interested in the related operators.

However, their usefulness seems to be mostly predicated on the idea that
you're doing a lot with class references or pointers which might be null and
where you would want to do an operation with them if they're non-null and
just skip that code if they're null. And given how little idiomatic D code
uses classes, I don't know how useful they'll be in practice in D. It's
likely to depend a lot on your coding style. I'm quite sure that I would
find them borderline useless, but some of the folks who are highly
interested in them may find a lot of use for them. I don't know.

- Jonathan M Davis



More information about the Digitalmars-d mailing list