var x:bool
proc showx() =
echo x
for x in [true, false]:
showx()
# output:
# false
# false
(Not saying current behaviour is wrong, I just thought I would mention it for others, because it did catch me out)
I think the behavior is just correct.
If you want to use the x from the for stmt you have to do something like this
var x:bool
proc showx(x: bool) =
echo x
for x in [true, false]:
showx(x)
With the exception that this example then does not makes use of the global var x in the for loop (and neither in the proc)
See also: http://forum.nim-lang.org/t/1990
AFAIK the reason why x is false twice is that the global x in the OP is bound to the showx() proc as closure. The x in the for loop is just not visible there.
See
var x: bool = true
proc showx() =
echo x
for n in [true, false]:
x = n
showx()
which does not what you expect but what would be "correct" because the x in the for loop is just available in the "for scope".
1. Are for-loop vars always new/temporary, even if they shadow another var?
Yes.
2. Should there be a warning if the for-loop var already exists (but won't be used)
Maybe. A good lint tool could check for it.
3. Is there a way to say you want to iterate using the existing var?
No.