I would like to take the contents of the G.myUglyList list here and pass it to the Outer.send() method. I do not understand why this gives a compiler error. ? extends Inner is the type I parameterize Outer with. So why is it rejecting an Inner passed to it? It wants a "? extends Inner", which isn't a type.
I want the list declared as List<Outer<? extends Inner>> so it can take subtypes of Inner. (Please see the edit below for why this is)
interface Outer<T> {
void send(T message);
}
interface Inner {}
interface Inner2 extends Inner {}
public class G {
List<Outer<? extends Inner>> myUglyList;
void foo() {
Inner xxx = null;
for (Outer<? extends Inner> outer : myUglyList) {
outer.send(xxx); //error
}
}
}
I get this error:
error: method send in interface Outer<T#2> cannot be applied to given types;
required: CAP#1
found: Inner<T#1>
reason: actual argument Inner<T#1> cannot be converted to CAP#1 by method invocation conversion
where T#1,T#2 are type-variables:
T#1 extends Object declared in class G
T#2 extends Object declared in interface Outer
where CAP#1 is a fresh type-variable:
CAP#1 extends Inner<T#1> from capture of ? extends Inner<T#1>
edit: I got a lot of answers saying just make the list of type List<Outer<Inner>>, but that is incorrect. I will not be able to add subtypes of Inner if I do that. If I try to add an Outer<Inner2>, it would fail. So list must be of type List<Outer<? extends Inner>>.
interface Inner2 extends Inner {}
class G {
void foo() {
Outer<Inner2> foiled = null;
myUglyList.add(foiled); //this will fail if list is of type List<Outer<Inner>>
Inner xxx = null;
for (Outer<? extends Inner> outer : myUglyList) {
outer.send(xxx); //error
}
Change your code for:
interface Outer<T> {
void send(T message);
}
interface Inner {}
interface Inner2 extends Inner {}
public class G {
List<Outer<Inner>> myUglyList;
void foo() {
Inner2 xxx = null;
for (Outer<Inner> outer : myUglyList) {
outer.send(xxx); //error
}
}
}
And it will compile
Update:
// No matter if you put 'T extends Inner' here, the 'add' won't compile
interface Outer<T extends Inner> {
void send(T message);
}
interface Inner {}
interface Inner2 extends Inner {}
public class G {
List<Outer<Inner>> myUglyList;
void foo() {
Outer<Inner2> foiled = null;
// This way, the 'add' invocation will compile, but it
// breaks the generic and generates a warning.
//
// Casting 'foiled' to (Outer<Inner>) will also fail
// because the compiler sees Outer<Inner2> as complete different type
// from Outer<Inner>
myUglyList.add((Outer) foiled);
Inner xxx = null;
for (Outer<Inner> outer : myUglyList) {
outer.send(xxx); //error
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With