An array destructing specification choice

Axel Rauschmayer axel at rauschma.de
Sat Nov 12 16:25:53 PST 2011


>>> Just to start with, what does this mean:
>>> 
>>>  [a,b,...r,c,d = [1,2,3]
>> 
>> That seems related to
>>     [a, b, c, d] = [1,2,3]
>> and to
>>     [a,b,...r,c,d] = [1,2,3,4]
>> 
>> Hence, I would guess:
>> a === 1
>> b === 2
>> r === []
>> c === 3
>> d === undefined
> 
> Why guess?

I’m not sure I understand.

> There is no requirement that ...r consume 1 element,

It consumes 0 elements above.

> but there is a requirement in Dave's desugaring that trailing non-rest element patterns consume elements up to the one indexed by rhs.length - 1.

But that doesn’t help if there are more trailing elements in the pattern than in the array to be destructured.

My definition would be:

[a1, ⋯ , an, ...r, b1, ⋯, bm] = [c1, ⋯, cl]

- n+m > l: r is bound to [], the non-rest pattern variables are bound as if there was no rest element:
    [a1, ⋯ , an, b1, ⋯, bm] = [c1, ⋯, cl]

- Otherwise r is bound to an array [r1, ⋯, rk] (with k = l-(n+m)) whose elements are determined via
    [a1, ⋯ , an, r1, ⋯, rk, b1, ⋯, bm] = [c1, ⋯, cl]

-- 
Dr. Axel Rauschmayer
axel at rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111113/d7325522/attachment.html>


More information about the es-discuss mailing list