T
Thomas 'PointedEars' Lahn
Lasse said:Dmitry A. Soshnikov said:Lasse said:The pattern
[x,y] = [e1,e2];
should be detectable at compile time, so the introduction of the
intermediate array can be optimized away.
Moreover, it can be done syntactically without brackets, as in Python:
No, it can't. Check your assumptions.
I'd prefer that.
The two of you are missing the ambiguity here which prevents that from
working as intended in JavaScript already. That is _not_ a destructuring
assignment in ECMAScript implementations: it is evaluating x, assigning e1
to y, and evaluating e2. Changing the semantics here would break a lot of
existing scripts, and is therefore not going to happen.
Or
(x,y) = (e1,e2); // except it's ambiguous with the stupid comma
operator.
That is why it should not be specified or implemented so; the LHS must
evaluate to y, and the RHS must evaluate to e2. The Mozilla people
(Brendan Eich?) picked the sensible way to provide this feature already.
But then, I think any modern type system should have arbitrary tuples.
But thanks to the comma operator, that syntax would not be backwards
compatible in ECMAScript implementations. It is highly doubtful that it
would be implemented so. So we will probably have to live with Array
initializers als tuple replacement.
Heck, I've seen languages with a swap primitive
x :=: y;
I cannot say I like that a lot either. I also do not think it is a pattern
useful often enough for it to deserve any special syntax; I have not used it
in production code for a decade or so, thanks to efficient built-in sort
implementations. The use of the destructuring assignment here is more of a
happy coincidence; the feature was designed for extracting elements from
Arrays instead.
PointedEars