bounded-wildcard

Upper bounded wildcards causing compilation error in Java

て烟熏妆下的殇ゞ 提交于 2020-03-23 12:09:09
问题 I cannot understand why I am getting these compilation errors: 1: The method add(capture#1-of ? extends Exec.Bird) in the type List is not applicable for the arguments (Exec.Sparrow) 2: The method add(capture#2-of ? extends Exec.Bird) in the type List is not applicable for the arguments (Exec.Bird) static class Bird{} static class Sparrow extends Bird{} public static void main(String[] args){ List<? extends Bird> birds = new ArrayList<Bird>(); birds.add(new Sparrow()); //#1 DOES NOT COMPILE

Why can't you have multiple interfaces in a bounded wildcard generic?

穿精又带淫゛_ 提交于 2020-01-26 11:23:48
问题 I know there's all sorts of counter-intuitive properties of Java's generic types. Here's one in particular that I don't understand, and which I'm hoping someone can explain to me. When specifying a type parameter for a class or interface, you can bound it so that it must implement multiple interfaces with public class Foo<T extends InterfaceA & InterfaceB> . However, if you're instantiating an actual object, this doesn't work anymore. List<? extends InterfaceA> is fine, but List<? extends

Why can't you have multiple interfaces in a bounded wildcard generic?

a 夏天 提交于 2020-01-26 11:23:37
问题 I know there's all sorts of counter-intuitive properties of Java's generic types. Here's one in particular that I don't understand, and which I'm hoping someone can explain to me. When specifying a type parameter for a class or interface, you can bound it so that it must implement multiple interfaces with public class Foo<T extends InterfaceA & InterfaceB> . However, if you're instantiating an actual object, this doesn't work anymore. List<? extends InterfaceA> is fine, but List<? extends

Bounded-wildcard related compiler error

淺唱寂寞╮ 提交于 2020-01-08 18:01:15
问题 I am wondering what is wrong with this code: Map <? extends String, ? extends Integer> m = null; Set<Map.Entry<? extends String, ? extends Integer>> s = m.entrySet(); The compiler complains with the error message: Type mismatch: cannot convert from Set<Map.Entry<capture#1-of ? extends String,capture#2-of ? extends Integer>> to Set<Map.Entry<? extends String,? extends Integer>> What should the type of s be? Eclipse suggests Set<?> but I am trying to get more specific than that. 回答1: This issue

Wildcard and type pameter bounds in java

一个人想着一个人 提交于 2020-01-03 10:59:12
问题 Consider this case: class A {} class B<T extends A, E extends T> { B<?, A> b; B<?, ? extends A> b2; } As I understand type bounds, in this case effective upper bounds of both T and E is class A . So the question: why javac doesn't accept class A as argument in declaration of field b , but accepts wildcard ? extends A in declaration of field b2 ? 回答1: With the following classes: class A {} class C extends A {} class B<T extends A, E extends T> {} Think of it like this: E extends T extends A

difference between creation unbounded and bounded wild card type array?

≡放荡痞女 提交于 2020-01-01 08:53:08
问题 Why is this code valid ArrayList<?>[] arr = new ArrayList<?>[2]; but the following two are not? ArrayList<? extends Object>[] arr = new ArrayList<? extends Object>[2]; ArrayList<? super Object>[] arr = new ArrayList<? super Object>[2]; The two last rows generate the compile error; error: generic array creation. Please clarify difference. update On the other hand ArrayList<?>[] arr = new ArrayList<?>[2]; compiles good but ArrayList<?> arr = new ArrayList<?>(); not. 回答1: There are a few issues

.NET equivalent for Java wildcard generics <?> with co- and contra- variance?

余生颓废 提交于 2019-12-30 09:59:08
问题 I'm stuck trying to translate some Java code that uses (bounded) wildcard generics to C#. My problem is, Java seems to allow a generic type to be both covariant and contravariant when used with a wildcard. For instance: Java: interface IInterf { } class Impl implements IInterf { } interface IGeneric1<T extends Impl> { void method1(IGeneric2<?> val); void method1WithParam(T val); } interface IGeneric2<T extends Impl> { void method2(IGeneric1<?> val); } abstract class Generic<T extends Impl>