An array destructing specification choice

Axel Rauschmayer axel at rauschma.de
Sat Nov 5 09:58:27 PDT 2011


To me the lhs indicates: array. Thus, the rhs is a pseudo-array. When used as such we get:
    > [].join.call({0:0, 1:1, length: 2, 2:2}, "-")
    '0-1'
Thus, 2 is invisible. That’s what I would expect, too, intuitively. I’d think that the assignment should reflect that (thus: z = undefined)

On Nov 5, 2011, at 17:44 , Brendan Eich wrote:

> On Nov 5, 2011, at 9:28 AM, Allen Wirfs-Brock wrote:
> 
>> In the following declaration, what should be the value of z?
>> 
>> let [z,y,z] = {0:0, 1:1, length: 2, 2:2};
>> 
>> should it be be 2 or undefined
>> 
>> undefined might be reasonable because it is an array pattern, and the source object is only has two "array-like" elements
>> 2 might be reasonable because the source object actually has a property named "2"
>> 
>> Which alternative will be least surprising to JS programmers?
> 
> What I implemented long ago in SpiderMonkey based on the ES4 proposal gives z=2. I still think that's the best answer.
> 
> Destructuring is "irrefutable" in that it desugars to assignments from properties of the RHS. It is not typed; it is not refutable (no one mention Erlang -- oops, Dmitry did; ok, refutable match is a separate beast, proposed but deferred: wiki.ecmascript.org/doku.php?id=strawman:pattern_matching). It should not impose any particular constraints on the RHS object based on the LHS pattern.
> 
> In this case, the RHS object is not consistent with Array invariants anyway. That means as much, if not more, than the array pattern. Someone may have set it up that way for a reason. It could have come from a JSON deserialization. The array pattern should not check for 'length' and enforce Array invariants that do not apply inherently on the RHS.
> 
> /be
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
> 

-- 
Dr. Axel Rauschmayer
axel at rauschma.de

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





More information about the es-discuss mailing list