groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Felix Dorner <>
Subject Calling each()/eachWithIndex() on an object that implements both Iterator and Iterable
Date Sat, 28 Nov 2015 00:25:02 GMT

I'm a new Groovy user. I am trying to use Groovy in the context of EMF ( This framework includes a utility class
ContentTreeIterator which implements Iterator and Iterable at the same
time. Clients are supposed to access instances through the Iterator
interface. (Using the iterator that's retuned by iterator() will yield
different results than using the object itself as the iterator). When I now
use each()/eachWithIndex() on instances of this class, the call goes


public static <T> Iterable<T> eachWithIndex(Iterable<T> self,
@ClosureParams(value=FromString.class, options="T,Integer") Closure
closure) {
        eachWithIndex(self.iterator(), closure);
        return self;

And I would like it to go directly to:

public static <T> Iterator<T> eachWithIndex(Iterator<T> self,
@ClosureParams(value=FromString.class, options="T,Integer") Closure
closure) {
        final Object[] args = new Object[2];
        int counter = 0;
        while (self.hasNext()) {
            args[0] =;
            args[1] = counter++;
        return self;

How can I tackle this?

Thanks for your help,

View raw message