Re: For loops
- Posted by John E <jwfe at t?lk?1.com> Nov 19, 2007
- 600 views
CChris wrote: > > John E wrote: > > > > Andy Drummond wrote: > > > > > > CChris wrote: > > > > > > > > Since there hasn't been much activity on this after some ideas wwere > > > > tossed, > > > > here would be my proposal for changes/extensions: > > > > > > > > Maybe there has been little response because FOR is OK as it is and > > doesn't need changing ;) > > > > If you read the forum for the past 7-8 days, you'll see that asimple bug > report > about trace(1) triggered some activity indeed, and some requests for > additions/changes. > The problem was that some of the changes were mutually incompatible, so there > was something to sort out. > > > I had indeed read most, but not all. I took your post to be a what you considered to be a summary, my mistake. I also misunderstood you when you said 'not much activity since...'. I'm not about to declare war over it :) But you know you are not just proposing this as a bug fix, but in fact you are proposing an innovation. > > > > 1/ Change to the for loop semantics <snip> > > > > So this would be like a for loop, but using an existing variable. > > > > > > > > Does this offer anything WHILE can't do? > > 1/ avoiding to forget to increment counter at the end; > 2/ protecting loop index from unwanted interference (by internally setting its > status to constant); > 3/ protecting limit and increment, just like in a for loop, if they are > (subscripted) > variables. Righto, some worthwhile protections against programmer error, but no enhanced ability. > > This is actually a variant of a for loop, rather than a while loop. Or a cross > breed between the two. The variant syntax "for with ..." was suggested instead > of "for_existing". It avoids a new keyword, but perhaps isn't as obvious for > the reader of the code. I certainly could live with it. > An underscore in keywords seems... wrong :) > > > > > > > > 3/ Change in ex.err dumps > > > > > > > > If a variable is defined several times in a scope, like for loop > > > > indexes, only > > > > the one defined last at the time of the dump will be displayed. This is > > > > a > change.</font></i> > > > > > > > > > > > > Expected code breakage: > > > > * none for 1/, since the change in 1/ will make legal code that > > > > currently > isn't.</font></i> > > > > * some (or perhaps none) with 2/: programs which define variable(s) > > > > named > > "for_existing"</font></i> > > > > will require this name be changed. > > > > * none for 3/, since code isn't involved. > > > > No performance hit is expected. > > > > > > > > > > > > Any comments? > > > > Since I expect to move this week, the changes, if agreed upon, won't be > > > > committed > > > > before next month. > > > > > > > > CChris > > > > > > For those who have followed my meanderings it will no surprise if I accept > > > this proposal. I just wondered why it would not be possible to write the > > > for_existing iterator within the loop. This agrees with current for-next > > > loop > > > coding, but I don't see why it needs be preserved. Otherwise, I concur > > > with > > > CChris's proposition. > > > > > > AndyD > > > > It is possible. But then what would be the difference with the modified for > loop? > The idea is that a for/for_existing loop uses a fixed increment to traverse > some fixed data structure or perform some operation. When it gets more dynamic > than that, use the while loop. That's my understanding at least. Currently, > you must use a while loop only beause you need some interaction with the > counter. > This doesn't enhance code readability. > > > > > The changes proposed, IMHO, amount to a work-around for the absence of > > local variables. Maybe add those? > > If by "local" you mean with nested, coder defined scope like in C, then yes, > the issue could be handled this way. This is in Advanced Euphoria already (see > below), as a new scope ... end scope block, at the top of which you can define > variables (local or private) that go out of scope on reaching the end scope > statement. > There hasn't been much demand for this feature, so I didn't plan to introduce > it in standard Eu. But I'd consider it a good thing. Didn't you already see > a flurry of private vars most of which are used only in some branch of an if > statement? Or reused accidentally a var that should have been scoped out long > ago? > > > > > If you want to go to the trouble, I suggest that adding a missing > > control feature such as a do..while would be a more appreciated use > > of your time. This might prove to be simpler to do, too. (Only > > guessing there). > > > > This was also suggested, but not much debated, as a do ... while loop is > strictly > equivalent to: > }}} <eucode> > while 1 do > -- something > if not <loop_again_condition> then exit end if > end while > </eucode> {{{ > > Equivalent, but less verbose and making the intent clearer. I have it > implemented > in my projet of modified interpreter - it may surface some day as Advanced > Euphoria, > but is low priority at the moment. > > The implementation of either is pretty simple actually, as it adapts the for > loop/while loop handling respectively. Scoped variables require adding an > extra > field to the IL - and preventing ? to display an out of scope for loop var > when > tracing could require it as well. This is a little harder, but can be done. > > CChris > > > > Regards, John I think I'll stick with V2.5. I think RDS got the language more or less right, that's why I bought a licence. All the best with your version anyway, CChris, you are obviously keen to get on and develop the language. No more posts from me. John