My brain hurts.  I love Iterable,  In this example, I'm trying to
define a Parent interface which iterates a sub-list of it's self
type.  But I want to be able to inherit to Child and change the
Iterator to Child.  Which in theory should work because Child is a sub
class of Parent.  I would expect any code which can operate on Child,
be able to operate on Parent.

My IDE thinks the "for (Parent p2 : parent)" loops are valid, but they
don't compile.  I think the problem is I'm not actually implementing
Parent with any Generic type info.. but I can't find any type
combinations which work.  And I'd prefer to not have the implementor
need to pass the type info.


interface Parent<T extends Parent<T>> extends Iterable<T> {
}

interface Child<T extends Child<T>> extends Parent<T> {
}

class ParentImpl implements Parent {
    public Iterator<Parent> iterator() {
        List<Parent> list = new ArrayList<Parent>();
        list.add(new ParentImpl());
        return list.iterator();
    }
}

class ChildImpl implements Child {
    public Iterator<Child> iterator() {
        List<Child> list = new ArrayList<Child>();
        list.add(new ChildImpl());
        return list.iterator();
    }
}

public class Tester {
    public static void main(String[] args) {
        Parent parent = new ParentImpl();
        for (Parent p2 : parent) {
            System.out.println("> " + p2);
            for (Parent p3 : p2) {
                System.out.println(">> " + p3);
            }
        }

        Parent p = parent.iterator().next();

        Child child = new ChildImpl();
        for (Child c2 : child) {
            System.out.println("> " + c2);
            for (Child c3 : c2) {
                System.out.println(">> " + c3);
            }
        }

        Child c = child.iterator().next();
    }
}


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to