Re: For loops
- Posted by Matt Lewis <matthewwalkerlewis at gmai?.?om> Nov 20, 2007
- 613 views
Andy Drummond wrote: > > c.k.lester wrote: > > > > Would this be too far outside Euphoric philosophy: > > }}} <eucode> > > for( x, x+1, x < 10) > > -- ... > > end for > > </eucode> {{{ > > Yes. way too far! I would prefer that what we have now continues to work. > I feel, having thought a bit, that CChris's idea to allow the scope of the > for loop iterator to persist beyond the loop as a constant is pretty good. > It allows the programmer access to the value of the iterator when the loop > decided to terminate (not always when the limit is reached) without doing > any harm to existing code. But that's just one way to do it. What about, say:
object in loop in = gets(fn) while sequence(in) by in = gets(fn) do -- ... end loop
We've got the same elements (initialization, condition for continuing and command for iteration) repackaged in a more euphorian manner. A simple alternative would be:
object in loop in = gets(fn) until atom(in) by in = gets(fn) do -- ... end loop
This is perhaps clearer, since it doesn't overload the while keyword, and is probably more similar to the current for-loop construct. Maybe both could be supported, though the use of 'not' in the condition would be equivalent. And maybe any and all of these 'subclauses' are optional, so that all of the following are valid (even if possibly nonsensical examples):
loop until atom(in) by in = gets(fn) do loop in = gets(fn) by in = gets(fn) do loop by in = gets(fn) do loop do
I don't think that any of these enhancements would require a change to the back end at all, since we're just putting things together differently. The IL code would probably map to a WHILE opcode, with different jump points set to inject the extra iteration code. > I also feel the continue statement would be valuable but I have never liked > > the choice of word. Would "next" be better? Implying that the next loop is to > commence immediately? I'm fine with either one. I think that this is a very worthy proposal (ooeu already has 'continue'). Matt