for-loop abstraction: How's this magic gonna work?

Gernot Neppert mcnepp02 at googlemail.com
Tue Jan 5 00:24:31 PST 2010


Hello,

while reading section 0.6b of the closures draft, I stumbled upon the
"loop abstraction" proposal.
The text provides this example of a loop abstraction:

<K,V,throws X>
void for eachEntry(Map<K,V> map, #void(K,V) throws X} block)
        throws X {
    for (Map.Entry<K,V> entry : map.entrySet()) {
        block.invoke(entry.getKey(), entry.getValue());
    }

}

Then it claims that the method "forEach" can later be used like this:

for eachEntry(String name, Integer value : map) {
        if ("end".equals(name)) break;
        if (name.startsWith("com.sun.")) continue;
        System.out.println(name + ":" + value);
    }

That leaves me puzzled: how's javac gonna figure out how to iterate
over the map when it's linking the above code against the classfile
containing the definition of "forEach"?

The supplied code in "forEach" that iterates over the map's entrySet
and the splits the Map.Entry into key and value is buried inside the
method's bytecode, right?

Or, rephrased after Darwin: Where's the missing link?


More information about the closures-dev mailing list