The Existential Operator

Dwayne rodneyd.teal at gmail.com
Tue May 20 04:20:02 PDT 2014


Claude Pache,

I had to read up on this existential operator to realize your question. In
your example the variable 'a' is the target/operand of the existential
operator. If 'a' isn't null or undefined then the result will be the result
that you'd expect had you not used the operator, assuming theres not a
TypeError in the expression. Here I choose to define an operator as a
shorthand function of which I don't find overly complicated.

function example() {
  If (a !== null && a !== void(0)) return a;
  return void(0);
}

If 'a' is undefined...

var a = void(0);
example(a).b.c

Would normally throw but with the existential operator...

var result = a?.b.c

Does not.

result === void(0) // true

Another thought.

var a = {};

var result = a?.b.c;

Will throw because there can be no property 'c' on undefined.

var result = a?.b?.c; <-- Awesome semantics!

And of course: result === void(0) // true


On Tue, May 20, 2014 at 3:30 AM, Aaron Powell <me at aaron-powell.com> wrote:

> It might be worthwhile keeping an eye on the C# language discussion on the
> same operator - http://roslyn.codeplex.com/discussions/540883
>
>
>
>
>
> *From:* es-discuss [mailto:es-discuss-bounces at mozilla.org] *On Behalf Of *A
> Matías Quezada
> *Sent:* Tuesday, 20 May 2014 7:56 PM
> *To:* Claude Pache
> *Cc:* es-discuss
> *Subject:* Re: The Existential Operator
>
>
>
> I think the current use of this operator will only make sense if the
> operator interrupts the whole sentence so
>
>
>
>     a?.b.c
>
> Will be the same as
>
>
>
>     a && a.b.c
>
>
>
> And
>
>
>
>     a?().b?.c?.d
>
>
>
> Will be same as
>
>
>
>     a && (x = a(), x.b && (x.b.c && x.b.c.d))
>
>
> ---
>
> A. Matías Quezada
>
> Senior Javascript Developer
>
> amatiasq at gmail.com
>
>
>
>
>
> 2014-05-20 11:31 GMT+02:00 Claude Pache <claude.pache at gmail.com>:
>
> Le 20 mai 2014 à 05:50, Dmitry Soshnikov <dmitry.soshnikov at gmail.com> a
> écrit :
>
>
> > Hi,
> >
> > (I remember, I mentioned this couple of years ago, but not sure about
> whether it was considered, etc)
> >
> > Will the "Existential Operator" for properly accessors be something
> interesting to consider for ES7 spec? Currently CoffeeScript uses it well.
> >
> > ```js
> > var street = user.address?.street;
> > ```
> >
> > The `street` is either the value of the `user.address.street` if the
> `address` property exists (or even if it's an object), or `null` /
> `undefined` otherwise.
> >
> > This (roughly) to contrast to:
> >
> > ```js
> > var street = user.address && user.address.street;
> > ```
> >
> > (the chain can be longer in many cases).
> >
> > The same goes with methods:
> >
> > ```js
> > var score = user.getPlan?().value?.score;
> > ```
> >
> > If potentially it could be interesting for ES7, I'll be glad helping
> with the proposal, grammar and algorithm (unless it was considered
> previously, and decided that it's not for ES for some reason).
> >
> > P.S.: I tried to solve this issue using default values of destructuring
> assignment, but it doesn't help actually.
> >
> > Dmitry
>
> Question: What is the semantics of the following:
>
>         a?.b.c
>
> Is it the same thing as
>
>         (a?.b).c
>         (a && a.b).c
>
> or the same thing as:
>
>         a && a.b.c
>
> (For the sake of the argument, just ignore the distinction between "falsy"
> and "null/undefined".)
> If it is the second option, I fear that the semantics of the so-called
> "existential operator" is more complicated than just an "operator".
>
> —Claude
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140520/2fd753f3/attachment.html>


More information about the es-discuss mailing list