2.6 feature request: foreach

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

I think that the next release should have something new and useful to the
language. Sure, you could do "continue" or "next" or something but will everyone
use them? When I was looking at all the code I've written I realized that maybe
around half of all my "for" loops were being used to reference each element in a
sequence one after another. Due to this I think that a "foreach" statement would
be a VERY useful addition to the language that I'm sure everyone would use.

It's syntax could go like so:

Instead of

sequence line
line = gets(0)
for i=1 to length(line)
    if line[i] = -- something
        -- code goes here
    end if
end for

One would type

sequence line
line = gets(0)
foreach element in line do
    if element = -- something
        -- code goes here
    end if
end foreach

Here "element" would implicitly be declared as an object and act just like the
current loop variable in "for". However, after every iteration of the loop
"element" will be set to the next next element in "line". A consideration would
be to optimize expressions of the type "foreach obj in reverse(seq)do"

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

Search



Quick Links

User menu

Not signed in.

Misc Menu