Wouldn't this be nice?

Reinier Zwitserloot reinier at zwitserloot.com
Wed Nov 11 09:37:31 PST 2009

Taking a page from python, why not add to one of the common dump spots for
functions, say, Collections, the following method:

public static void Iterable<Integer> range(final int i) {
    return new Iterable<Integer>() {
        public Iterator<Integer> iterator() {
            return new Iterator<Integer>() {
                private int p = 0;
                public boolean hasNext() {
                    return p < i;
                public Integer next() {
                    if (p < i) return p++;
                    throw new NoSuchElementException();
                public void remove() {
                    throw new MethodNotSupportedException();

then all you need to do is:

import static java.util.Collections.range;

for (int i : range(50)) { /* do stuff */ }

Josh: I'm a bit meh on your suggestion. Is that really so much of an
improvement over:

for (char c : "someString".toCharArray())?

probably yes, but between the extra burden on the JLS and potential
confusion between chars and codepoints, it doesn't strike me as a big loss.

--Reinier Zwitserloot

On Wed, Nov 11, 2009 at 5:36 PM, Paulo Levi <i30817 at gmail.com> wrote:

> for(int i : infos.length)
> for(long i : 35L)
> or even
> for(int i : -30)
> (0 being always the start, the second element the end.)
> I think it would. It would make the for loop actually useful if
> iterating over more than one (indexed) data structure in the same
> loop.
> The ideas come late and later.

More information about the coin-dev mailing list