moonscript icon indicating copy to clipboard operation
moonscript copied to clipboard

Multiple assignment of same value

Open ckknight opened this issue 13 years ago • 4 comments

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.

ckknight avatar Feb 19 '12 08:02 ckknight

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.

leafo avatar Feb 19 '12 15:02 leafo

Sure, but it can compile to

local localValue
localValue = somethingElse
someTable.name = localValue

zah avatar Mar 07 '12 10:03 zah

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?

ggVGc avatar Jun 08 '14 16:06 ggVGc

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)

SoniEx2 avatar Jan 31 '15 18:01 SoniEx2