次のコードでは、R が Appendable を拡張しているため、R が期待される場所で Appendable を返すことはできませんか?
/**
* Produces an R, to which a T has been semantically appended,
* whatever that may mean for the given type.
*/
interface Appendable <R, T>
{
/**
* Append is not expected to modify this Appendable,
* but rather to return an R which is the result
* of the append.
*/
R append(T t);
}
interface PluralAppendable <R extends Appendable<R, T>, T>
extends Appendable<R, T>
{
default R append(T... els)
{
// Easier to debug than folding in a single statement
Appendable<R, T> result = this;
for(T t : els)
result = result.append(t);
/* Error: Incompatible types.
Required: R
Found: Appendable<R, T> */
return result;
}
}