An array destructing specification choice

Allen Wirfs-Brock allen at wirfs-brock.com
Sun Nov 6 09:09:34 PST 2011


On Nov 6, 2011, at 8:03 AM, John J Barton wrote:

> On Sat, Nov 5, 2011 at 11:16 AM, Allen Wirfs-Brock
> <allen at wirfs-brock.com> wrote:
>> 
>> 
>> What do you think this returns:
>>     Array.prototype.pop.call({0:0, 1:1, length: 2, 2:2})
> 
> The answer depends upon the definition of pop(). For an array argument
> we can predict based on our experience with pop(). Otherwise we cannot
> predict without more information. That's the nature of dynamically
> typed language.


The definition of Array.prototype.pop is precisely provided by section 15.4.4.6 of the the ECMAScript specification.  That definition applies whether or not the "this value" of the function is a "real" array or just an ordinary object.  

> 
> Consequently this example does not bear upon the thread in my opinion.
> In the example
>  let [z,y,z] = {0:0, 1:1, length: 2, 2:2};
> the language is inserting some operation to map the RHS to the LHS.
> The answer depends upon the operation inserted; it need not be related
> to pop().

The relevance is that pop is an example of how the ES specification consistently defines apparent array operations and functions in a manner that applies to all objects, not just instances of the Array constructor.

You said 

>> But the feature does not do this. Rather, since LHS is array, it
>> coerces the RHS to an array:
>>  let z = coerceToArray(_rhs)[0];
>> and we don't know what that operation means.
> 


I an showing, via pop, that we do know what it means to treat a regular object as an array.

Also, There is no actual "coerceToArray" operation in the ES specification and such a coercion would not occur for destructuring.  Instead, very operation/function that might access/operate upon an "array" is defined in such a way that it result is well defined for any object. 

The ES spec. must define what happens in all cases.  That is exactly the exercise we are engaged in here.  How do we specify destructuring so that its behavior is both well defined in all situations and consistent with both other related ports of the language and with reasonable user expectations.

Allen


More information about the es-discuss mailing list