Generics: multiple interfaces as return parameter does not work

Discussion in 'Java' started by martinus, Nov 19, 2007.

  1. martinus

    martinus Guest

    Hi all, I have implemented an interface that looks somewhat like this:

    public interface LocalOptimizer {
    <X extends List<Node> & RandomAccess> void optimize(final X tour);
    <X extends List<Node> & RandomAccess> X doSomething();
    }

    The method optimize() requires a class that implements List<Node> and
    RandomAccess, for example ArrayList does this. This works great, I can
    call it like this:

    optimize(new ArrayList<Node>());

    But I cannot use the second method, this code does not compile:

    public <X extends List<Node> & RandomAccess> X doSomething() {
    return new ArrayList<Node>();
    }

    I get the error message "Type mismatch: cannot convert from
    ArrayList<Node> to X"
    Any ideas how I can make this work?

    Martin
     
    martinus, Nov 19, 2007
    #1
    1. Advertising

  2. martinus

    Lew Guest

    martinus wrote:
    > Hi all, I have implemented an interface that looks somewhat like this:
    >
    > public interface LocalOptimizer {
    > <X extends List<Node> & RandomAccess> void optimize(final X tour);
    > <X extends List<Node> & RandomAccess> X doSomething();
    > }
    >
    > The method optimize() requires a class that implements List<Node> and
    > RandomAccess, for example ArrayList does this. This works great, I can
    > call it like this:
    >
    > optimize(new ArrayList<Node>());
    >
    > But I cannot use the second method, this code does not compile:
    >
    > public <X extends List<Node> & RandomAccess> X doSomething() {
    > return new ArrayList<Node>();
    > }
    >
    > I get the error message "Type mismatch: cannot convert from
    > ArrayList<Node> to X"
    > Any ideas how I can make this work?


    Generic methods require an argument to allow the compiler to resolve what X is.

    A better way might be to make the interface itself generic:

    public interface LocalOptimizer< T extends List<Node> & RandomAccess>
    {
    void optimize( final T tour );
    T doSomething():
    }

    public class Foo extends LocalOptimizer<ArrayList<Node>>
    {
    public optimize( ArrayList<Node> nodes ) { ... }
    public ArrayList<Node> doSomething(){ ... }
    }

    You could also drop the generics altogether, at least for doSomething().

    public ArrayList<Node> doSomething();

    Do you really need the return type to be *any* List that is RandomAccess? You
    could just insist that it be an ArrayList.

    I wouldn't suggest using Vector, but perhaps you don't care whether it's an
    ArrayList or a Stack. OTOH, ArrayList and Stack are not usually
    interchangeable in an algorithm. Chances are that the only useful type for
    these methods is ArrayList. If so, you can drop the generics altogether and
    just use that.

    --
    Lew
     
    Lew, Nov 19, 2007
    #2
    1. Advertising

  3. martinus

    Daniel Pitts Guest

    martinus wrote:
    > Hi all, I have implemented an interface that looks somewhat like this:
    >
    > public interface LocalOptimizer {
    > <X extends List<Node> & RandomAccess> void optimize(final X tour);
    > <X extends List<Node> & RandomAccess> X doSomething();
    > }
    >
    > The method optimize() requires a class that implements List<Node> and
    > RandomAccess, for example ArrayList does this. This works great, I can
    > call it like this:
    >
    > optimize(new ArrayList<Node>());
    >
    > But I cannot use the second method, this code does not compile:
    >
    > public <X extends List<Node> & RandomAccess> X doSomething() {
    > return new ArrayList<Node>();
    > }
    >
    > I get the error message "Type mismatch: cannot convert from
    > ArrayList<Node> to X"
    > Any ideas how I can make this work?
    >
    > Martin

    There was a discussion about this some time ago.
    Also, I've written an article about this.

    <http://virtualinfinity.net/wordpress/java/esoteric-java-features/2007/03/06/type-intersection-in-java-or-interest-in-interfaces-is-invaluable/>

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Nov 19, 2007
    #3
  4. martinus

    martinus Guest

    > There was a discussion about this some time ago.
    > Also, I've written an article about this.
    >
    > <http://virtualinfinity.net/wordpress/java/esoteric-java-features/2007...>


    Thanks!

    --
    Martin
     
    martinus, Nov 20, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Elliot
    Replies:
    10
    Views:
    1,156
    Robert Elliot
    Jun 6, 2004
  2. Juergen Berchtel
    Replies:
    1
    Views:
    6,012
    John C. Bollinger
    May 20, 2005
  3. Greenhorn
    Replies:
    15
    Views:
    839
    Keith Thompson
    Mar 6, 2005
  4. Soul
    Replies:
    0
    Views:
    527
  5. Graeme
    Replies:
    8
    Views:
    341
    Bill McCleary
    Aug 6, 2009
Loading...

Share This Page