Destructuring syntax infelicity viz. identifier vs. property name

# Dominic Cooney (15 years ago)

The proposed syntax for destructuring objects at < harmony:destructuring> is:

Pattern ::= "{" (Field ("," Field)* ","?)? "}" (array destructuring elided) Element ::= Pattern | LValue Field ::= Identifier (":" Element)? LValue ::= <any lvalue expression allowed in a normal assignment expression>

This misses the opportunity for some parallelism with object literals. For example, I can construct {' 0blah': x} with an object literal, but I can't access the ' 0blah' with object destructuring.

Maybe Field ::= Identifier (":" Element)? could be changed to:

Field ::= PropertyName (":" Element)?

(using ECMA-262's PropertyName) to permit {' 0blah': x} = ...

This also allows seeming nonsense like {' 0blah'}, but I'm not sure that is so bad since bindings in objects can have names that aren't identifiers, so why not in scopes in general, too? Although destructuring could no longer be implemented via desugaring because there's no way to make a binding with an arbitrary name in the current scope.

So to preserve desugarability you could spell out the alternatives of PropertyName in Field, to require elements with strings and numbers:

Field ::= Identifier (":" Element)? | StringLiteral ":" Element | NumericLiteral ":" Element

Dominic

# Brendan Eich (15 years ago)

On Oct 21, 2010, at 10:58 PM, Dominic Cooney wrote:

The proposed syntax for destructuring objects at harmony:destructuring is:

Pattern ::= "{" (Field ("," Field)* ","?)? "}" (array destructuring elided) Element ::= Pattern | LValue Field ::= Identifier (":" Element)? LValue ::= <any lvalue expression allowed in a normal assignment expression>

This misses the opportunity for some parallelism with object literals. For example, I can construct {' 0blah': x} with an object literal, but I can't access the ' 0blah' with object destructuring.

This is just another (still not formal enough, obviously not yet not carefully spec'ed :-|) grammar bug, copied from the ES4 destructuring proposal page. SpiderMonkey and Rhino get it right:

js> var {"0": x} = {"0":42}

js> x

42 js> var {"0blah": z} = {"0blah":42}

js> z

42

Maybe Field ::= Identifier (":" Element)? could be changed to:

Field ::= PropertyName (":" Element)?

(using ECMA-262's PropertyName) to permit {' 0blah': x} = ...

This also allows seeming nonsense like {' 0blah'}, but I'm not sure that is so bad since bindings in objects can have names that aren't identifiers, so why not in scopes in general, too?

No, that goes too far. There's no way to name the binding. It's not a property of any object. Again, SpiderMonkey:

js> var {"0blah"} = {"0blah":42}

typein:1: SyntaxError: missing variable name: typein:1: var {"0blah"} = {"0blah":42} typein:1: .....^

So to preserve desugarability you could spell out the alternatives of PropertyName in Field, to require elements with strings and numbers:

Field ::= Identifier (":" Element)? | StringLiteral ":" Element | NumericLiteral ":" Element

Right, that's the way to go. I'll make sure the wiki page is updated again. Thanks,

# Dominic Cooney (15 years ago)

Is it helpful to keep picking at these nits?

Dominic

# Brendan Eich (15 years ago)

On Oct 21, 2010, at 11:18 PM, Dominic Cooney wrote:

Is it helpful to keep picking at these nits?

Sure, much appreciated. These have to get fixed sooner or later, sooner's fine. Thanks again,