I am trying to understand why the compiler is unable to resolve the bar
method call. I would expect bar(Xyz::new)
to always select bar(Supplier)
as bar(T extends Xyz)
can never match due to the upper bound on Xyz
.
public <T extends Xyz> void foo(T s) {}
public <T extends Xyz> void bar(T s) {}
public <T extends Xyz> void bar(Supplier<T> s) {}
public void example() {
foo(Xyz::new); // not valid (does not extend Xyz)
bar((Supplier<Xyz>) Xyz::new); // valid (explicitly a Supplier)
bar(Xyz::new); // ambiguous - but only one method is valid?
}
public static class Xyz {}
If bar(T)
is not applicable, even when alone (as shown with foo(T)
), then surely the only option is bar(Supplier)
making this a non-ambiguous overload.
Why is the bar
call ambiguous, especially when the foo
and bar(T)
calls are not valid resolutions themselves?
Runnable example of above code: https://www.jdoodle.com/ia/kqP