Re: FOR i ...
- Posted by Andy Drummond <andy at kestre??ele.com> Nov 15, 2007
- 463 views
CChris wrote: > > Chris Bensler wrote: > > > > Would it break any code if i were simply allowed to exist after the end of > > the > > loop? > > > > for i = 1 to 2 do > > end for > > for i = i to 4 do > > end for > > > > Chris Bensler > > Code is Alchemy > > I don't think it would, but then Pete's proposal of allowing a predeclared > integer > i won't work (or will do with subtle side effects, which is worse). > The parser will have to decide whether an existing i is a predeclared non loop > variable - probably disallowed, cf CK -, a loop index at the same level - > allowed > and reusing that index - or a nesting loop index - disallowed -. > > As a result, the i defined last before ex.err is generated will be dumped > there, > whether the loop it belongs to was active or no longer is, with its last > value. > Not worse that the current state of things. > > This wouldn't add another construct to the language, but change the semantics > of an existing one. Not my own preference, because _both_ semantics are useful > to have. > > CChris To re-iterate what I wrote on an entirely inappropriate subject, why is it not possible/reasonable to simply say that if the for-loop variable is declared prior to the loop then it is used - and remains in scope after the loop exits - and if the variable does NOT exist then the behaviour is exactly as it is now in that the variable is in scope only for the duration of the for-next loop. This is consistent with C-style languages if you consider the loop to be a procedure. This MIGHT break existing programs but only if written in a way that uses the same variable name outside the loop as inside, with no relation between them - a pretty poor way of writing code anyway, IMHO. I guess if you tried to use a non-atomic variable you would have to raise an error but that is right anyway. AndyD