9 Replies Latest reply on May 14, 2007 9:53 PM by 807606

    Implemented Iterable<E>, but can not Use Foreach

    807606
      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?
        • 1. Re: Implemented Iterable<E>, but can not Use Foreach
          807606
          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) {
          
                  }
              }
          }
          • 2. Re: Implemented Iterable<E>, but can not Use Foreach
            807606
            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
            • 3. Re: Implemented Iterable<E>, but can not Use Foreach
              807606
              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 :-)
              • 4. Re: Implemented Iterable<E>, but can not Use Foreach
                807606
                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) {
                
                        }
                    }
                }
                • 5. Re: Implemented Iterable<E>, but can not Use Foreach
                  807606
                  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
                  • 6. Re: Implemented Iterable<E>, but can not Use Foreach
                    807606
                    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)
                    {
                    }
                    }
                    ...
                    • 7. Re: Implemented Iterable<E>, but can not Use Foreach
                      807606
                      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;
                           }
                      }
                      • 8. Re: Implemented Iterable<E>, but can not Use Foreach
                        807606
                        I am using javac with 1.5.0_11
                        • 9. Re: Implemented Iterable<E>, but can not Use Foreach
                          807606
                          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.