On Nov 6, 2011, at 6:32 AM, Axel Rauschmayer wrote:

> One more thought: Should the following assignments produce the same result?
> 
>      [x, y, z] = {length:0, 0:0, 1:1, 2:2}
>      [x, y, z] = [].slice.call({length:2, 0:0, 1:1, 2:2})

did you intend to use the same "length" value in both of the above lives?

> 
> Then the decision boils down to whether an array conversion happens (however 
> implicitly) on the rhs or whether the lhs is syntactic sugar for
>      x = rhs[0], y = rhs[1], z = rhs[2]

As I've mentioned in a reply to another message.  There are currently no "array 
conversions"  in ES.  Instead, Array operations/functions are always defined to 
in a manner that applies to all objects, not just instances of the Array 
constructor.

Here is another example of why desugaring to [ ] is to simplistic a view of  
destructuring:

let [a=0, b=1] = [,"one"];

Note the default value initializers to the right of the declared identifiers.  
Such initializers provide the value when the RHS of the initializer for the 
entire destructuring pattern does not.

What is the value of the variable 'a' after this statement?  Is it undefined or 
is it 0?

If destructuring initialization is a simple desugaring to [ ] access then the 
above means the same as:

let _rhs =  [,"one"];
let a=_rhs[0], b=_rhs[1]; 

and the value of 'a' will be undefined.

Note that there was no reason to include the =0 or =1 default initializers 
anywhere because  _rhs[X] yields a value  for all possible values of X.  For 
most values of X, that will be the value undefined.

If we want such default value initializers to actually mean anything we have to 
use a more complex definition of  destructuring.  Approximately:

function hasProperty(obj,p) {
    // code that is equivalent to the ES5 [[HasProperty]] internal method
    // return obj.[[HasProperty]](p)
}

let _rhs =  [,"one"];
let a= (hasProperty(_rhs,0) ? _rhs[0] : 0),  b=(hasProperty(_rhs,1) ? _rhs[0] : 
1); 


The spec. I'm writing already takes care of all of these cases.  My main point 
above, is that thinking of desugaring as just a simple assignment is a naive 
view that doesn't take into account all the bells and whistles that have been 
added to it since Lars' original implementation at Opera.

Allen




_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to