destructuring: as patterns?

Herby Vojčík herby at mailbox.sk
Wed Apr 18 14:05:48 PDT 2012


Allen Wirfs-Brock wrote:
> On Apr 18, 2012, at 1:45 PM, Herby Vojčík wrote:
>
>> Brendan Eich wrote:
>>> Allen Wirfs-Brock wrote:
>>>>> For array patterns we would need to allow property assignments in
>>>>> array literals:
>>>>>>> let [b, 0:{x,y}] = ...;
>>>>>>> This was proposed at one point, IIRC, but a while ago.
>>> Of course, one could destructure like so:
>>>
>>> let {0: b, 0: {x, y}} = ...;
>>>
>>>
>>>> It was also suggested that we allow iterators/generators to supply the
>>>> initialization values to array destructurings. This currently isn't in
>>>> the draft spec. and I think it would introduce some fairly significant
>>>> specification and practical complications (eg, rest bindings and
>>>> open-ended generators).
>>> Destructuring patterns should be static. I don't see a conflict if we
>>> stick to this rule, but given the ability to use an object pattern, I
>>> don't see a need for the array literal element label syntax either.
>> As was already pointed out, problems are not only _array_ destructurings, but more argument list destructrings, where if you want to destructure 0-th argument both as b and as {x,y}, you would need something like that, since you already are inside list, you cannot opt for object.
>
>
> function f({b},...rest) {
>     let {x,y}=b;
>     ...
> }
>
> doesn't seem like too much of a burden

It was an example to show the problem in principle, it's not that this 
concrete example is the problem... if you use object destructuring on 
array, you cannot use spread to catch the rest - and on the other hand, 
if you use array destructuring, you must then do additional let's like 
the one before.

Not a big problem, but it would be nicer if these compromises would not 
need to be there.

Possible solution for both object and array destructing at the same time 
would be to allow use comma-expression-like destructurings:

   let {b:(b,{x,y})} = obj;
   let [(b,{x,y}), ...rest] = [obj, ...];
   function f((b,{x,y}),foo,bar) { ... }

The object notation is nicer and more readable imo, but this solves the 
problem for any destructuring.

> Allen

Herby


More information about the es-discuss mailing list