moonscript
moonscript copied to clipboard
Multiple assignment of same value
The following is possible in lua, but causes a parse error in MoonScript. It could be handy, as I use the pattern in lua frequently.
a = b = c
Which sets both a and b to c.
For an actual use case,
someTable.name = localValue = somethingElse
Which, ideally would turn into
local localValue
someTable.name = localValue = somethingElse
or something along those lines.
This actually isn't possible in lua.
> a = b = c
stdin:1: unexpected symbol near '='
Assignment isn't an expression (unlike a lot of other languages). Right now assignment isn't an expression in MoonScript either but I'm thinking of making it a pseudo expression in some places. I definitely think this kind of syntax is useful.
Sure, but it can compile to
local localValue
localValue = somethingElse
someTable.name = localValue
I think this is a very useful and much needed feature. After using MS for a few days, I have already come across several instances where I would have wanted it. Is there some specific reason assignment is not an expression currently?
Here's a different use case:
local temp
if (temp = t.a) and (temp = temp.b) and (temp = temp.c)
Which would translate to
local temp;
temp = t.a
if temp then
temp = temp.b
if temp then
temp = temp.c
if temp then
Which's A LOT faster than
if t.a and t.a.b and t.a.b.c then
(especially for really long chains)