Implemented Iterable<E>, but can not Use Foreach

Consider the interface:

==================

public interface ReadOnlySet<E> extends Iterable<E>

{

..

public Iterator<E> iterator();

..

}

==================

Now when I try:

========================================

ReadOnlySet<Vertex><Set><K>>> parentScopes = _scopes.getAdjacentVertices(scope);

for(Vertex<Set><K>> parentScope : parentScopes)

{

========================================

The compiler complains:

Error: foreach not applicable to expression type

What am I not seeing?

[647 byte] By [DejasPerPera] at [2007-11-27 4:21:00]
# 1

The forum is misformatting your post! Try putting spaces of either side of any < or >.

This compiles for me:

import java.util.*;

interface ReadOnlySet<E> extends Iterable<E> {

public Iterator<E> iterator();

}

class K {}

class Vertex < T > {}

public class ReadOnlySetTest {

void m(ReadOnlySet < Vertex < Set < K > > > set) {

for( Vertex < Set < K > > v : set) {

}

}

}

Hippolytea at 2007-7-12 9:28:08 > top of Java-index,Java Essentials,Java Programming...
# 2
In your example, what type is K, with which you've parameterized that Set? I'm betting it's not a concrete type, but an undeclared type variable. It needs to be an actual type, or it needs to be a type variable of the class or method where that foreach is declared
georgemca at 2007-7-12 9:28:08 > top of Java-index,Java Essentials,Java Programming...
# 3

> The forum is misformatting your post! Try putting

> spaces of either side of any < or >.

>

> This compiles for me:

> > import java.util.*;

>

> interface ReadOnlySet<E> extends Iterable<E> {

>public Iterator<E> iterator();

>

> class K {}

>

> class Vertex < T > {}

>

> public class ReadOnlySetTest {

> void m(ReadOnlySet < Vertex < Set < K > > > set)

> {

>for( Vertex < Set < K > > v : set) {

>}

> }

>

>

You've cheated! You've defined a class called K :-)

georgemca at 2007-7-12 9:28:08 > top of Java-index,Java Essentials,Java Programming...
# 4

> You've cheated! You've defined a class called K :-)

Guilty. Here it is a parameter.

import java.util.*;

interface ReadOnlySet<E> extends Iterable<E> {

public Iterator<E> iterator();

}

class Vertex < T > {}

public class ReadOnlySetTest < K > {

void m(ReadOnlySet < Vertex < Set < K > > > set) {

for( Vertex < Set < K > > v : set) {

}

}

}

Hippolytea at 2007-7-12 9:28:08 > top of Java-index,Java Essentials,Java Programming...
# 5

> > You've cheated! You've defined a class called K

> :-)

>

> Guilty. Here it is a parameter.

> > import java.util.*;

>

> interface ReadOnlySet<E> extends Iterable<E> {

>public Iterator<E> iterator();

>

> class Vertex < T > {}

>

> public class ReadOnlySetTest < K > {

> void m(ReadOnlySet < Vertex < Set < K > > > set)

> {

>for( Vertex < Set < K > > v : set) {

>}

> }

>

>

I'm betting the OP has another, raw method, in a raw class, that he's trying to use K with. eg, following from your code

class Whirly {

void doSomething() {

ReadOnlySet < Vertex > < Set < K > > > parentScopes = _scopes.getAdjacentVertices(scope);

for(Vertex < Set > < K > > parentScope : parentScopes)

{

// etc

}

}

Although Eclipse doesn't give the error he reported

georgemca at 2007-7-12 9:28:08 > top of Java-index,Java Essentials,Java Programming...
# 6

K is parameterized. So:

public class ScopeGraph<K,V>

{

...

protected V lookUpHelp(K key, Vertex<Set><K>> scope) throws NoSuchVertexException, NoSuchValueException

{

...

for(Vertex<Set><K>> parentScope : parentScopes)

{

try

{

return lookUpHelp(key, parentScope);

}

catch(NoSuchValueException e)

{

}

}

...

DejasPerPera at 2007-7-12 9:28:08 > top of Java-index,Java Essentials,Java Programming...
# 7

No idea then. The following compiles fine for me in Eclipse, I don't have a javac on this machine at the moment. Which compiler are you using?

import java.util.Set;

public class ScopeGraph<K, V> {

private Scopes _scopes;

protected V lookUpHelp(K key, Vertex<Set><K>> scope)

throws NoSuchVertexException, NoSuchValueException {

ReadOnlySet<Vertex><Set><K>>> parentScopes = _scopes.getAdjacentVertices(scope);

for (Vertex<Set><K>> parentScope : parentScopes) {

try {

return lookUpHelp(key, parentScope);

} catch (NoSuchValueException e) {

}

}

return null;

}

}

georgemca at 2007-7-12 9:28:08 > top of Java-index,Java Essentials,Java Programming...
# 8
I am using javac with 1.5.0_11
DejasPerPera at 2007-7-12 9:28:08 > top of Java-index,Java Essentials,Java Programming...
# 9

Now that's just silly. I changed the code to:

Iterator<Vertex><Set><K>>> parentScopes = _scopes.getAdjacentVertices(scope).iterator();;

for(Vertex<Set><K>> parentScope : parentScopes)

{

and it still won't compile. I really should be able to foreach over an iterator.

DejasPerPera at 2007-7-12 9:28:08 > top of Java-index,Java Essentials,Java Programming...