Existential operator (was: ||= is much needed?)

Brendan Eich brendan at mozilla.org
Thu Jun 21 08:40:42 PDT 2012


John Tamplin wrote:
> On Thu, Jun 21, 2012 at 11:23 AM, Brendan Eich <brendan at mozilla.org 
> <mailto:brendan at mozilla.org>> wrote:
>
>     John Tamplin wrote:
>
>         So do you have to do it by treating the ?. operator as a
>         standalone?
>
>     Not sure what you mean here -- "standalone ?" (with an
>     English-langauge ? implied after in your sentence? ;-)?
>
>     Or something else that amounts to a concealed Reference or
>     Nil-value-proxy expression?
>
>     Just talking spec here: as Allen mentioned, ECMA-262 specifies
>     semantics by evaluating productions in a mostly-LR(1) grammar, so
>     member and call expressions (left-associative) have to result in
>     some kind of (spec-internal or language-external) value.
>
>     Thus foo?.bar.baz.quux is really (((foo?.bar).baz).quux).
>
>
> Yes, and I am suggesting during parsing it gets treated as:
>
> (((foo?.bar)?.baz)?.quux)
>
> because we saw a ?. operator earlier in the expression.

Ok, but this doesn't seem observable, or necessary in the spec.

>  That way you don't have to get the Coffeescript behavior by 
> introducing some fake object or a MaybeReference,

CoffeeScript does not introduce a fake object or MaybeReference:

$ cat /tmp/soak.coffee
o = null
console.log(o?.p.q.r.s.t)

$ ./bin/coffee -p !$
./bin/coffee -p /tmp/soak.coffee
(function() {
   var o;

   o = null;

   console.log(o != null ? o.p.q.r.s.t : void 0);

}).call(this);

Rather, ECMA-262 would need to elaborate its internal Reference type as 
Allen wrote.

> and you just implement the ?. operator as always returning the LHS if 
> it is null/undef.

Note also that CoffeeScript does not short-circuit to the 
unbound/null/undefined LHS "value", it always uses void 0 -- AKA undefined.

>     Indeed one can translate when parsing. CoffeeScript does this,
>     with some separate passes for its other purposes (implicitly
>     declared variables, indentation-based block structure, etc.).
>
>     The ES specs can't do this, though, not without a total rewrite.
>
>
> I realize it would be changes, but then so would adding a 
> MaybeReference.  Just thought I would bring it up.

Allen's right, MaybeReference is a much (much!) smaller change, 
especially if we want to support the ?( and suffix-? variants.

/be


More information about the es-discuss mailing list