#1 void passOn(Consumer<Animal> consumer, Supplier<Animal> supplier)#2 <A extends Animal> void passOn(Consumer<A> consumer, Supplier<? extends A> supplier)#3 <A extends Animal> void passOn(Consumer<? super A> consumer, Supplier<A> supplier)#4 <A extends Animal> void passOn(Consumer<? super A> consumer, Supplier<? extends A> supplier)
(Consumer<Runnable>, Supplier<Dog>) while Dog is subtype of Animal & Runnable
A
has a range of choices. It might be confusing to the programmer,
regardless of how spec/compiler behaves. It gets more confusing when the
arguments are implicit lambda expressions. Also very confusing if
consumer is Consumer<Runnable>, since the programmer may not be able to follow the exact inference rules.A is easily fixed; nothing confusing/difficult to the programmer. The method can be considered an extended instance method on Supplier<A extends Animal>,
so the lack of wildcard is justified. This case might be an evidence
that wildcard is not always desirable on a naturally covariant type.#5 void passOn(Consumer<? super Animal> consumer, Supplier<? extends Animal> supplier)In the body of the method, rather than getting an A from the supplier and passing it to the consumer, you just get an Animal from the supplier and pass it to the consumer.
I don't think you gain anything by declaring the "A" parameter, since the compiler can always infer A to Animal anyway, turning #4 into the equivalent of #5.--
You received this message because you are subscribed to the Google Groups "java.lang.fans" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-lang-fan...@googlegroups.com.
To post to this group, send email to java-la...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/java-lang-fans/CACuKZqG8z%3DQYp6d3mN%2B3aaxkW8ZC%3D3Oi5nNZ_6G_S7cBVnTdSA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Why do you even need a named type parameter at all? You're not using it in the return argument or anything.#5 void passOn(Consumer<? super Animal> consumer, Supplier<? extends Animal> supplier)In the body of the method, rather than getting an A from the supplier and passing it to the consumer, you just get an Animal from the supplier and pass it to the consumer.I don't think you gain anything by declaring the "A" parameter, since the compiler can always infer A to Animal anyway, turning #4 into the equivalent of #5.