tags:

views:

259

answers:

3

I've looked through similar questions and have come up short, so here it goes;

public interface Predicate<T>{
    boolean apply(T t);
}
public interface Function<F, T>{
    T apply(F f);
}

public class ConcretePredicate extends Predicate<Foo>, Function<Bar, Boolean>{
    @Override
    public boolean apply(Foo foo){/*stuff*/}
    @Override
    public Boolean apply(Bar bar){/*stuff*/}
} 

ConcretePredicate shows the error,

"Name clash: The method apply(T) of type Predicate has the same erasure as apply(F) of type Function but does not override it"

It looks like it should be working though, Anyone have any ideas as to what is going on?


[Edit] So it looks like this is an issue with eclipse, Galileo does not show this error, while Helios does. I've submitted a bug report with eclipse and will update once I get a response.

[Edit] Changed to a simpler case that shows the same error but removes confusion about erasure.

A: 

What's going on is type erasure.

To get this to work you need to change the name of one of your apply methods.

Skip Head
+1  A: 

The code DOES work. Here it is slightly rewritten to be in one class for ideone.com:

interface Predicate<T>{
    boolean apply(T t);
}
interface Function<F, T>{
    T apply(F f);
}
class Foo {}
class Bar {}
class Two<K, V>{
    private K k;
    private V v;
    public Two(K k, V v) {
        this.k = k;
        this.v = v;
    }
    public K getKey() {return k;}
    public V getValue() {return v;}
}

Then here comes the good stuff:

abstract class NewPredicate
implements Predicate<Foo>, Function<Two<Foo, Bar>, Boolean>{
}

class ConcretePredicate extends NewPredicate{
    @Override
    public boolean apply(Foo foo){ return false; }
    @Override
    public Boolean apply(Two<Foo, Bar> two){ return null; }
} 

public class Main {
public static void main(String[] args) {
    System.out.println(new ConcretePredicate());
}
}

The code compiles and runs fine (as seen on ideone.com, Eclipse and javac 1.6.0_17).


Related questions

These are related to actual "has the same erasure as ... but does not override it" compiler error message:

polygenelubricants
Why is eclipse giving me this error then? Is it just an issue with the IDE?
Andrew
@Andrew: you are saying that the code from ideone.com, taken as a whole and put into one `Main` class, doesn't compile in your Eclipse? Which version?
polygenelubricants
Also related http://stackoverflow.com/questions/1827890/compiler-difference-between-intellij-and-eclipse
polygenelubricants
Sorry for the confusion, I'm saying that eclipse is showing this as an error, It still compiles and runs, but I don't understand why it is showing up as an error.
Andrew
@Andrew: if it's an error then it wouldn't compile and run. I'm confused now. We are talking about my version of the code on ideone.com, with the `k/key/v/value` correction, and the `return` statement in the body of the `@Override`, right?
polygenelubricants
Also related: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6182950 : methods clash algorithm should not depend on return type ; according to this, it seems that the code should NOT have compiled, if I'm reading this correctly...
polygenelubricants
I would think it should compile, given that one of the `apply` methods takes a `Foo` parameter while the other takes a `Two`.
ColinD
@ColinD: yes, the `javap` confirms this too, but the supposedly faulty code in the bug report compiles for me too, and the `javap` is absolutely insane with both `int f(List)` and `double f(List)` methods in the same class. I am utterly baffled, but I'm trying to investigate this as much as I can. But I do think your argument is correct... I hope...
polygenelubricants
@polygenelubricants When I copy your code into my eclipse an run it I get an error,Exception in thread "main" java.lang.Error: Unresolved compilation problems: Name clash: The method apply(F) of type Test.Function<F,T> has the same erasure as apply(T) of type Test.Predicate<T> but does not override it Name clash: The method apply(T) of type Test.Predicate<T> has the same erasure as apply(F) of type Test.Function<F,T> but does not override it at ...and it does not run. So yeah it does not compile for me locally.
Andrew
Ok, so jdk1.6.0_13 shows the error, but jdk1.6.0_20 does not, so something changed to fix it I guess
Andrew
@polygenelubricants Maybe I'm not understanding you correctly, but the situation from that bug report and the situation here are completely different. In the bug report, the two methods both take an argument that has the same erasure (`List`) where in this case the two methods take completely different types of arguments (`Foo` and `Two`).
ColinD
That said, you can make a class that `implements Predicate<T>, Function<F, Boolean>` and then create an instance of it with `<Integer, Integer>` or any identical types as `T` and `F`, resulting in two methods with the exact same signature... hm.
ColinD
A: 

I have submitted a bug report with the eclipse team and they are still arguing about the semantics of the JLS and what it means, but it would seem my issue is indeed a bug in eclipse Helios. Looks like I'm going to have to downgrade to Galileo until this is resolved.

Bugzilla Entry

Andrew