Destructuring assignment
On Aug 21, 2008, at 8:23 PM, David-Sarah Hopwood wrote:
Brendan Eich wrote:
On Aug 21, 2008, at 6:47 PM, Mark S. Miller wrote:
On Thu, Aug 21, 2008 at 6:34 PM, Brendan Eich <brendan at mozilla.org> wrote:
js> [d,e,f] = foo() 1,2,3 Is this a simultaneous assignment of d,e,f, or does it declare variables d,e,f?
The first.
If the first, can you do a swap as
[d, e] = [e, d]
Sure can (variable values as in previous mail): [...]
So the rule is that the RHS is evaluated, and then the assignments are done left-to-right, correct? And each array element (or property value in the case of an object literal) on the left is syntactically a LeftHandSideExpression?
Yes. In the binding forms, each element must be an identifier.
(Sounds fine, I just want to confirm my understanding.)
doku.php? id=proposals:destructuring_assignment doku.php? id=discussion:destructuring_assignment
Brendan Eich wrote:
On Aug 21, 2008, at 8:23 PM, David-Sarah Hopwood wrote:
Brendan Eich wrote:
On Aug 21, 2008, at 6:47 PM, Mark S. Miller wrote:
On Thu, Aug 21, 2008 at 6:34 PM, Brendan Eich <brendan at mozilla.org> wrote:
js> [d,e,f] = foo() 1,2,3 Is this a simultaneous assignment of d,e,f, or does it declare variables d,e,f?
The first.
If the first, can you do a swap as
[d, e] = [e, d]
Sure can (variable values as in previous mail): [...]
So the rule is that the RHS is evaluated, and then the assignments are done left-to-right, correct? And each array element (or property value in the case of an object literal) on the left is syntactically a LeftHandSideExpression?
Yes. In the binding forms, each element must be an identifier.
(Sounds fine, I just want to confirm my understanding.)
proposals:destructuring_assignment, discussion:destructuring_assignment
There is a comment at the end of the discussion page saying:
We decided against object property shorthand in destructuring such as:
{ x, y, z } = { x: 1, y: 2, z: 3 }
This would be nice and concise, but possibly so terse as to be confusing.
Part of the justification of destructuring is that its syntax exactly
mimics that of structuring.
— Dave Herman 2006/10/20 12:24
and the syntax on the proposal page does not include this shorthand.
I don't have a particularly strong opinion on this (because it has no semantic implications and the desugaring is trivial), but is the shorthand proposed for ES-Harmony and/or ES3.1, or not?
Presumably, if it is proposed, then the syntax of 'field' using ES3 production names becomes:
field ::= PropertyName ":" LeftHandSideExpression | Identifier
assuming that is is possible to mix the shorthand and longhand forms for different fields.
On Aug 21, 2008, at 10:02 PM, David-Sarah Hopwood wrote:
There is a comment at the end of the discussion page saying:
We decided against object property shorthand in destructuring
such as:
{ x, y, z } = { x: 1, y: 2, z: 3 }
This would be nice and concise, but possibly so terse as to be
confusing.
Part of the justification of destructuring is that its syntax
exactly
mimics that of structuring.
— Dave Herman 2006/10/20 12:24
That's out of date. Writing object destructuring binding patterns
often since then, in building the ES4 RI with its self-hosted built-
ins, and the ESC self-hosted compiler on Tamarin, led us to the
strong desire for the short-hand. The rest of the RI was in SML,
which has the same sort of destructuring pattern, which closed the deal.
Sorry no one updated the wiki -- clearly, you have a point in noting
the lack of a community to tend pages.
and the syntax on the proposal page does not include this shorthand.
In JS1.8 in Firefox 3.
I don't have a particularly strong opinion on this (because it has no semantic implications and the desugaring is trivial), but is the shorthand proposed for ES-Harmony and/or ES3.1, or not?
ES3.1 is out in draft spec form, was originally hoped to be done at
end of this year, should be done by next spring with interoperable
implementations based on its all-but-done drafts, and is not
including anything other than the Array extras and property getters
and setters from JS1.x.
So, Harmony.
assuming that is is possible to mix the shorthand and longhand forms for different fields.
js> function bar() { return {p:42, q:"hi"} }
js> var {p, q:r} = bar()
js> p
42 js> r
hi
Brendan Eich wrote:
On Aug 21, 2008, at 10:02 PM, David-Sarah Hopwood wrote:
[...]
and the syntax on the proposal page does not include this shorthand.
In JS1.8 in Firefox 3.
Is there a specification of JS1.8 with an EBNF syntax for the extensions? Google didn't turn up anything of that kind immediately.
(Thanks for the other answers.)
Brendan Eich wrote:
So the rule is that the RHS is evaluated, and then the assignments are done left-to-right, correct? And each array element (or property value in the case of an object literal) on the left is syntactically a LeftHandSideExpression?
(Sounds fine, I just want to confirm my understanding.)