Re: 2.6 feature request: foreach

new topic     » goto parent     » topic index » view thread      » older message » newer message

Robert Craig wrote:
> 
> Thanks for the suggestion.
> I thought about something like this about 10 years ago.
> At first it looked like a good idea, but after trying
> various examples and thinking it through, I felt it
> might cause confusion. Short simple examples like the one
> above are ok, but what if you have a loop with 100 statements
> in it? The aliasing of one name for another might be hard to remember.
> In the middle of a long section of code you could easily
> forget that element = 99 really means (something like) line[i] = 99
> (I assume you could read or write these loop variables).
> Or element[5] += 1, really means (more or less) line[i][5] += 1
> And could you change line in the middle of the loop? Would the
> value of line be locked in. What if you saw:
> line[j] = element
> Would you instantly understand what it meant?
> These issues made me think the feature was not
> such a good thing to add to a language that is supposed to
> be simple.
> 
> Thanks,
>    Rob Craig
>    Rapid Deployment Software
>    <a href="http://www.RapidEuphoria.com">http://www.RapidEuphoria.com</a>

Well, since the loop variable when using "for" is read-only I don't see a reason
why "foreach" would have the loop variable read-write. Yes, you are right
"element[5] = 675" would be confusing if it just expanded to "line[i][5] = 675"
since you might not remember what it expanded to but that would make it the only
statement in the langauge that had behavior like that which would probably
confuse people. However, keeping it read-only would keep it in-line with the
current "for" statement and you'd still be able to loop through sequence elements
quickly. Also, why would you need writeability to every element of a sequence
when we already can do operations on every element of a sequence? In many cases
it'd just be redundant and take up more code. If you absolutely did need to do it
you could just do it the way we do it already which would further distinguish
between reading from evrey element of a sequence using a loop and modifying every
one. Yes, it would limit the "foreach" statements power but it would still be
useful.

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu