crystal-community / icr

Interactive console for Crystal programming language
MIT License
505 stars 40 forks source link

Behavioral difference when reassigning variable in multi-line blocks #38

Closed dennisjbell closed 7 years ago

dennisjbell commented 7 years ago

While I was pleasantly surprised that icr supported multiline blocks, there does seem to be some lingering issues. Specifically, I have found that if your first usage of a variable in a block is assigning that variable using the same variable in the RHS, regardless of that variable already being assigned outside the block, it causes an error:

icr(0.20.0) > i = 0
 => 0
icr(0.20.0) > 10.times do |a|
icr(0.20.0) >   i = i + a
can't use variable name 'i' inside assignment to variable 'i'
icr(0.20.0) >
icr(0.20.0) > 10.times {|a| i = i + a}
 => 10

As you can see, doing it all in a single line has no issue.

I'm assuming this is because icr is detecting that you're in a block and trying to validate the inner code stand-alone. If this is the case, I would recommend simply not validating until the outer-most block is exited.

greyblake commented 7 years ago

Thanks for the report. I guess I know, there the problem is, and we'll try to fix it as soon as I have some free time:)

jwoertink commented 7 years ago

@greyblake Not sure if you saw, but this commit ^ fixes this issue.

greyblake commented 7 years ago

That is fixed, thank you @jwoertink !