Overload Ambiguity

Zhong Yu zhong.j.yu at gmail.com
Fri Jul 12 15:03:09 PDT 2013

Consider these three methods

    public interface CompletionStage<T>

       CompletionStage<Void> thenRun(Runnable action)

       CompletionStage<Void> thenAccept(Consumer<? super T> action)

        <U> CompletionStage<U> thenApply(Function<? super T,? extends U> fn)

Would it be safe to overload them, e.g. name all three as "then()"?
(assuming that's a good idea)

       <U> then(T->U)

Could this overloaded design cause any ambiguity anywhere, or cause
other kinds of problems?


Another question. Consider these two methods

    interface Foo<T>

#1    <R> Foo<R> map( F1<T, R> );
#2    <R> Foo<R> map( F2<T, Foo<R>> );  // AKA "flat map"

where F1 and F2 are exactly like Function, and there's no subtyping
relationship between the two.

The two map() methods may create ambiguity when both are applicable to
a lambda expression (that returns a Foo<?>). However, is there a
notion that #2 is more specific than #1, therefore #2 is chosen over

(Analogy - according to JLS7, #4 is more specific than #3

#3    <R> void foo(R)
#4    <R> void foo(List<R>)

intuitively whenever #4 is applicable, #3 is applicable. So is there a
similar rule for cases like #1 and #2?)

If the answer is no, I guess we can always make F2<:F1, which makes #2
more specific than #1, correct?

Zhong Yu

More information about the lambda-dev mailing list