fail-fast object destructuring (don't add more slop to sloppy mode)

Brendan Eich brendan at mozilla.com
Wed Jan 2 12:35:20 PST 2013


Herby Vojčík wrote:
> Brendan Eich wrote:
>> Herby Vojčík wrote:
>>> That is, these are identical, except the syntax:
>>>
>>> r = o.p.q {p: {q: r}} = o
>>> r = o.?p.q {?p: {q: r}} = o
>>> P=o.p; Q=o.?q {p: P, ?q: Q} = o
>>
>> Here I part company only on syntax:
>>
>> r = o?.p.q {p?: {q: r}} = o
>> P=o.p; Q=o?.q {p: P, q?: Q} = o
>>
>> And of course, the short-hand works:
>>
>> p=o.p; q=o?.q {p, q?} = o
>
> Oh, now that I see details, I do not like the syntax, because the 
> symmetry of use of ? vanished.
>
> Compare:
>     r = o.?p.q    {?p: {q: r}} = o    // in both forms, "?p"
> vs,
>     r = o?.p.q    {p?: {q: r}} = o    // 1st: "o?"; 2nd: "p?"

You're right, and in CoffeeScript o?.p tolerates null or undefined o.

Given eo.cs:

o = null
o?.p
o?.p.q

`coffee -p eo.cs` shows

(function() {
   var o;

   o = null;

   if (o != null) {
     o.p;
   }

   if (o != null) {
     o.p.q;
   }

}).call(this);

So the ? in ?. really does modify its left operand.

It would be bad to diverge from CoffeeScript by standardizing some other 
?. in ES7.

ISTM your equivalence was slightly broken -- but we can fix it:

r = o.p?.q <==>  {p?: {q: r}} = o

To tolerate o = null and o = undefined (which is not what o.p?.q does), 
the equivalence would be:

r = o?.p.q <==>  {p: {q: r}}? = o

I think this all works and makes sense. On the left, ? goes after the 
leading object or would-be object, denoted o. On the right, ? goes after 
the outermost object pattern.

Suffix vs. prefix should be an independent design decision, because ? as 
prefix should work the same on both sides, ergo, transposing around the 
operand to make ? a suffix should work the same on both sides.

/be


More information about the es-discuss mailing list