coffeescript icon indicating copy to clipboard operation
coffeescript copied to clipboard

Destructuring Compound Assignment

Open OnesimusUnbound opened this issue 15 years ago • 11 comments

Honestly, I could not think of a realistic use case for the use of destructuring compound assignment, but personally, I think it's a good idea, hence I posted it.

[a, b, c] ?= [1, 2, 3]
[a, b, c] += [1, 2, 3]

OnesimusUnbound avatar Mar 24 '11 00:03 OnesimusUnbound

Without a good use case, this is going to be pretty dead in the water. At least, judging by past issues. jashkenas is very concerned with bloat and/or fluff.

michaelficarra avatar Mar 24 '11 01:03 michaelficarra

Here's an obvious use case.

satyr avatar Mar 24 '11 03:03 satyr

I agree with satyr, I’ve been working on a small physics engine and being able to do something like [x, y, z] += [velocityX, velocityY velocityZ] would be a boon

robb avatar Mar 24 '11 14:03 robb

Yeah, those look like pretty good use cases. Now let's try to get an expected compilation. How do we treat differently sized arrays? How about variables (on either side: a += [0, 1], [a,b] += c)?

The only problem I see is that we won't support a = [0, 1, 2] + [3. 4, 5]. That will create a pretty confusing inconsistency, since compound assignments work but the regular operations by themselves don't.

michaelficarra avatar Mar 24 '11 15:03 michaelficarra

As for a += [0, 1], assuming a = [1, 1, 1], I think the result for a should be [1, 2, 1]. // a += [0, 1] var a, _ref, _ref2; _ref = a[0], _ref2 = a[1], a[0] = _ref + 0, a[1] = _ref2 + 1;

and [a, b] += c // [a,b] += c var a, b, c, _ref, _ref2; _ref = c[0], _ref2 = c[1], a += _ref, b += ref2;

Currently, if the number of variables in the lefthand compound exceeds the number on the right, they still get all assigned, e.g.

[a, b, c, d] = [1, 2, 3, 4]
[a, b, c, d] = ['a', 'b', 'c']

console.log d # undefined

Not sure if this is a good thing. Following that logic, for [a, b, c] += [1, 2], c would be undefined.

But why couldn’t [a, b, c] = [1, 2] get compiled to var a, b, c, _ref; _ref = [1, 2], a = _ref[0], b = _ref[1]; leaving c unaffected. That would lead to a = [0, 1, 2, x] + [3, 4, 5] getting compiled to // [0, 1, 2, x] + [3, 4, 5] var a, x, _ref; a = (_ref = [0 + 3, 1 + 4, 2 + 5, x], _ref);

and [a, b, c, d] += [1, 2, 3] would result in // [a, b, c, d] += [1, 2, 3] var a, b, c, d, _ref; _ref = [a + 1, b + 2, c + 3, d], a = _ref[0], b = _ref[1], c = _ref[2], d = _ref[3]; with d still retaining its original value.

robb avatar Mar 26 '11 14:03 robb

I'm going to have to vote against this issue for now because of all the inconsistencies this would introduce. If JS was strictly typed, this would be a sure thing. But we can't have _ += _ behave differently than _ = _ + _ and behave differently when given a variable in place of an array literal. -1

michaelficarra avatar Mar 26 '11 15:03 michaelficarra

I made a mistake in my last post, [a, b, c] = [1, 2] must leave c as undefined to be consistent with [a, b, c] = someArray, rendering the rest of the post moot.

Another attempt

// [a, b, c] = array
a = array[0], b = array[1], c = array[2];

// [a, b, c] + array
_ref = [a + array[0], b + array[1], c + array[2]];

// array + [a, b, c]
_ref = [array[0] + a, array[1] + b, array[2] + c];

// array += [a, b, c]
_ref = [array[0] + a, array[1] + b, array[2] + c];
array = _ref;

// [a, b, c] += array
_ref = [a + array[0], b + array[1], c + array[2]];
a = _ref[0], b = _ref[1], c = _ref[2];

// [a, b, c] += [x, y, z]
_ref  = [x, y, z];
_ref2 = [a + _ref[0], b + _ref[1], c + _ref[2]];
a = _ref2[0], b = _ref2[1], c = _ref2[2];

// [a, b, c] += [x, y]
_ref  = [x, y];
_ref2 = [a + _ref[0], b + _ref[1], c + _ref[2]];
a = _ref2[0], b = _ref2[1], c = _ref2[2]; // c is now NaN as _ref[2] was undefined

This appears to work just as well with the other arithmetic and binary operators. Also seems pretty consistent to me, what do you think?

robb avatar Mar 26 '11 17:03 robb

Enabling this proposal is just a matter of propagating the operator on desugar.

  • [a, b] = c => a = c[0]; b = c[1]
  • [a, b] += c => a += c[0]; b += c[1]

As for a += [0, 1], it's not really a destructuring; would require operator overloading or something.

we can't have _ += _ behave differently than _ = _ + _ and behave differently when given a variable in place of an array literal

True, but doesn't apply to destructuring: a = [b]; a = c and [b] = c should never be the same.

satyr avatar Mar 26 '11 17:03 satyr

Thanks for sharing your thoughts. The reason I posted this idea is so that anyone can share their view (pros and cons).

For now, one disadvantage that I see is how will this construct handle.

[a b, c] = [1, 2, 3]
[a, b..., c] += [1, 2, 3, 4, 5]
# a = 2, c = 8, b = ?

OnesimusUnbound avatar Mar 29 '11 02:03 OnesimusUnbound

That boils down to something like

a += 1
c += 5
b += [2, 3, 4]

which, as @satyr mentioned earlier, is not really a destructuring and dependent on the type of b. I don’t think it has to be a supported use case, though.

robb avatar Apr 14 '11 10:04 robb

The proposed generated code below looked like vector math

// [a, b, c] + array
_ref = [a + array[0], b + array[1], c + array[2]];

// array + [a, b, c]
_ref = [array[0] + a, array[1] + b, array[2] + c];

However, JS's Array is "an array is an ordered set of values associated with a single variable name[1]". Rather, I think it's more natural to concatenate the addition of two arrays, just as with ruby and python.

// [a, b, c] + array
_ref = [a, b, c].concat(array);

// array + [a, b, c]
_ref = array.concat([a, b, c]);

Strangely when I use + for arrays

print typeof ([1, 2, 3] + [4, 5, 6])
=> string.

The concatenation of the string version of the two arrays was returned o0! Strange JS . . .

Let's fix it by using appropriate operation based on the type of the leftmost object!

At compile time, it's possible to check if the left most object is an array or number or string and apply the appropriate operation.

[1, 2, 3] + a 
=> [1, 2, 3].concat(a)
"Hello" + a 
=> "Hello" + a
1 + a 
=> 1 + a

However, when the left most part of the addition operation is a variable, we can just sniff the type for the basic JS type.

a + b

will be

var __ref;
switch(__type(a)) {
  case "String": 
    __ref = a + b; 
    break;

  case "Number": 
    __ref = a + b; 
    break;

  case "Array": 
    __ref = a.concat(b); 
    break;
}

Wait, the code looks too complicated for addition! gee :-(. When we have the following expression

a + b + c + ....

Then we're generating a very large code just for addition. And we've not yet covered subtraction, multiplication, etc . . .

Ok, let us just stick to array + array = string to make it simple.

And I think this is the best apporach just to be close with JS.

Anyway, how about destructuring compound assignment?

Personally, I like the idea. After all, we can do much with that expression, especially with this one

[a, b, c] ?= [1, 2, 3]

Rather than

a ?= 1
b ?= 2
c ?= 3

or

a ?= 1; b ?= 2; c ?= 3

[1] https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array#Description

OnesimusUnbound avatar Apr 16 '11 06:04 OnesimusUnbound