Re: loop do .. until end loop is surely incorrect!
- Posted by mattlewis (admin) Feb 24, 2011
- 2157 views
If what Matt says is the case then the construct should be:
loop do ... integer x = 3 ... until x=3
no end loop, and scope ends at until. The declaration is incorrectly used at until x = 3.
I don't recall how exactly the syntax came about, but I suspect that there was a preference to keep the normal euphorian end ... construct. More in keeping with the keyword ... do ... end keyword, it could have been:
loop do end until <condition> loop
That's also somewhat awkward, and I'm not sure if it's really better.
This is counter-intuitive as the wording is to end loop which is what until x = 3 is.
Note: i's scope in for i .. end for ends with the exit from the loop.
For loop variables are special cases, and always have been. They're not true variables, in any case. Try assigning to them, for instance.
If the loop is unrolled the code fails as there would be multiple redefinitions of the declared variable(s) in the resultant code. The scope would be lost along with the until - or rather the scope would have to be retained which means the loop cannot be unrolled.
Yet it should always be possible to unroll a loop.
This is oversimplifying, or maybe just trying to hard to come up with an objection. There's no reason why you couldn't unroll a loop, but as you might expect, more complex loops with declarations in them are more complex to unroll.
One way declaration within a loop could work correctly is with a block:
loop do ... def integer x = 3 ... end def ... until ..
But that is really to say: no declaration in loops.
Well, that construct is exactly what we have, except that you don't have to add def ... end def. It's all taken care of with the normal loop constructs. I've said it before, but I'll say it again: our scoping here is very similar to C-like languages, except that in euphoria, we have stuff like do...end instead of curly braces.
Matt