javaeclipsejavac

Java Generics: Who is right, javac or Eclipse compile?


Calling this method:

public static @Nonnull <TV, TG extends TV> Maybe<TV> something(final @Nonnull TG value) {
    return new Maybe<TV>(value);
}

like this:

public @Nonnull Maybe<Foo> visit() {
    return Maybe.something(new BarExtendsFoo());
}

compiles just fine in Eclipse, but javac gives an "incompatable types" warning:

found   : BarExtendsFoo

required: Foo


Solution

  • I do not understand why javac did not infer the correct type,
    but you can help the compiler by supplying the types as in

    public @Nonnull Maybe<Foo> visit() {
        return Maybe.<Foo, BarExtendsFoo>something(new BarExtendsFoo());
    }