chkn / AluminumLua

NOTE: THIS WAS A TOY PROJECT AND IS NOT MAINTAINED
100 stars 13 forks source link

else statement variable context #11

Open leslie-linden opened 10 years ago

leslie-linden commented 10 years ago

Hi. I've noticed an issue with variable contexts in "else" statements. The following code:

bar = 0 function foo() print "bar is " .. bar if bar > 0 then bar = 0 else bar = bar + 1 end end foo() foo() foo() foo()

Has the following output: bar is 0 bar is 1 bar is 0 bar is 2

The "else" block seems to have its own local context for the "bar" variable which is remembered each time the block is executed. I wasn't able to figure out any workaround to this issue in the general case, although the above code can be edited to make the problem go away by not using an else statement.

Thanks again.