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?
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) {
}
}
}
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
> 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 :-)
> 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) {
}
}
}
> > 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
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)
{
}
}
...
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;
}
}
I am using javac with 1.5.0_11
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.