moonscript icon indicating copy to clipboard operation
moonscript copied to clipboard

Allow update assignment on multiple values

Open s-ol opened this issue 7 years ago • 6 comments

e.g.

a, b = 1, 2
a, b += 3, 4
print a, b # 4, 6

I think this would be very handy, especially working with tuples like 'light' vectors (x,y).

maybe even

a, b = 1, 2
a, b += 3
print a, b # 4 5

though I am not sure how useful and idomatic that would be.

s-ol avatar Oct 15 '18 04:10 s-ol

maybe even

a, b = 1, 2
a, b += 3
print a, b # 4 5

absolutely not, because:

x = ()-> 1, 2
a, b += (x!)
a, b += x!

You'd have to be aware every time of the vararg list, which can cause confusions. I tend to have similar issues with the string library and would hate to see the opposite issue with update assignment.

-- EDIT --

I realized I did not elaborate.

a, b += x!

must compile to:

_asn_0, _asn_1 = x()
if _asn_1 ~= nil then
  x = x + _asn_1
else
  x = x + _asn_0
end
y = y + _asn_0

This means that you'd have to compare it each time, even after you've compiled to Lua.

RyanSquared avatar Oct 16 '18 02:10 RyanSquared

@RyanSquared good catch on the multi-returns, I felt like there was something wrong with that syntax but didn't figure it out.

Your elaborated Lua is the behaviour that you don't want to see, right?

s-ol avatar Oct 16 '18 03:10 s-ol

I'd like to not have the syntax at all because I'd prefer for MoonScript to be as little overhead as possible; this would involve an overhead which might not be necessary.

Additionally, I'm not fond of the syntax at all as it tends to lead to confusion.

RyanSquared avatar Oct 16 '18 04:10 RyanSquared

I'd like to not have the syntax at all because I'd prefer for MoonScript to be as little overhead as possible; this would involve an overhead which might not be necessary.

Additionally, I'm not fond of the syntax at all as it tends to lead to confusion.

well i think the syntax should not involve any checking and be plain sugar with a matched amount of names / values on both sides.

s-ol avatar Oct 16 '18 04:10 s-ol

The issue is that it is literally impossible to have a matched amount of values on the RHS unless you completely get rid of vararg returns. Alternatively you can assert() return values from a vararg return but that also has overhead.

RyanSquared avatar Oct 16 '18 06:10 RyanSquared

The issue is that it is literally impossible to have a matched amount of values on the RHS unless you completely get rid of vararg returns. Alternatively you can assert() return values from a vararg return but that also has overhead.

why? you just transform

a, b, c, d [OP]= [ANYTHING]

into

local _tmp_a, _tmp_b, _tmp_c, _tmp_d = [ANYTHING]
a, b, c, d = a OP _tmp_a, b OP _tmp_b, c OP _tmp_c, d OP _tmp_d

if you make a mistake, there will be an error thrown (operation on nil value) at runtime, as there should be, since this is a runtime problem occuring.

s-ol avatar Oct 16 '18 07:10 s-ol