Ambiguous reference

Dávid Karnok akarnokd at gmail.com
Wed Nov 13 06:01:46 PST 2013


Hello,

I'm rewriting a library and run into a strange ambiguity error reported by
javac in JDK 8 b115, Windows 7 x86:

public class Bug<T> {
    public void forEach(Consumer<? super T> consumer) {
        // ...
    }
    public void forEach(BiConsumer<? super Integer, ? super T> consumer) {
        forEach((v) -> consumer.accept(1, v));
    }
    public void forEach(Predicate<? super T> stoppableConsumer) {
        // ...
    }
    public void forEach(BiPredicate<? super Integer, ? super T>
stoppableConsumer) {
        forEach(v -> stoppableConsumer.test(1, v));
    }
}

error: reference to forEach is ambiguous
        forEach((v) -> consumer.accept(1, v));
  both method forEach(Consumer<? super T>) in Bug and method
forEach(Predicate<? super T>) in Bug match
  where T is a type-variable:
    T extends Object declared in class Bug

error: reference to forEach is ambiguous
        forEach(v -> stoppableConsumer.test(1, v));
  both method forEach(Consumer<? super T>) in Bug and method
forEach(Predicate<? super T>) in Bug match
  where T is a type-variable:
    T extends Object declared in class Bug

I'm using NetBeans 7.4 (Build 201310111528) which doesn't report this
ambiguity in the editor, and the "highlight occurrences" correctly selects
the 3rd method when I select the forEach in the 4th method. However, the
clean and build fails with the errors above. I have to add explicit cast to
the problematic places.

So I'm a bit confused right now. Is it a bug with NetBeans' editor, with
javac or simply the artifact of how overload resolution works?
-- 
Best regards,
David Karnok


More information about the lambda-dev mailing list