Crystal: Multiple constant assignment regression

Created on 24 Oct 2016  路  4Comments  路  Source: crystal-lang/crystal

FOO, BAR = 1, 2
FOO # => undefined constant FOO

This was fixed after being reported reported in #958 but it's failing again with a different error message.

bug compiler

Most helpful comment

Because this:

FOO, BAR = 1, 2

Is equivalent to this:

tmp1 = 1
tmp2 = 2
FOO = tmp1
BAR = tmp2

Multi-assign is expanded this way to make this possible:

a, b = b, a

and you need to store the right-hand side in a temporary variable first.

But that doesn't work with constants, because constants don't have access to local variables, they have their own scope.

I also think that this actually makes the code a bit harder to read. And constants are used much less frequently than local variables. We _could_ change the rule and simply assign values one by one in a multi-assign, just for constants, but I'm not sure it's worth it. So the easiest thing to do is to remove this from the language.

All 4 comments

Good catch. I will remove this "feature" from the language

@asterite i agree with you. Removing this will be better 馃憤

Why remove it?

Because this:

FOO, BAR = 1, 2

Is equivalent to this:

tmp1 = 1
tmp2 = 2
FOO = tmp1
BAR = tmp2

Multi-assign is expanded this way to make this possible:

a, b = b, a

and you need to store the right-hand side in a temporary variable first.

But that doesn't work with constants, because constants don't have access to local variables, they have their own scope.

I also think that this actually makes the code a bit harder to read. And constants are used much less frequently than local variables. We _could_ change the rule and simply assign values one by one in a multi-assign, just for constants, but I'm not sure it's worth it. So the easiest thing to do is to remove this from the language.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

straight-shoota picture straight-shoota  路  91Comments

xtagon picture xtagon  路  132Comments

asterite picture asterite  路  71Comments

RX14 picture RX14  路  62Comments

asterite picture asterite  路  70Comments