moonscript
moonscript copied to clipboard
Allow update assignment on multiple values
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.
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 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?
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.
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.
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.
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.