Community
Participate
Working Groups
Below a couple of unclear result of assigmnemt checks with wildcards. import java.util.List; public class A<U extends Number> { List<? extends Number> lhs; List<? extends U> rhs; { lhs.add(rhs.get(0)); } } produces a compile error. However "? extends U".isAssignmentCompatible("? extends Number") answer true.
Another case: List<? extends Object> lhs; List<? extends Number> rhs; { lhs.add(rhs.get(0)); } procudes compile error. However "? extends Number".isAssignmentCompatible("? extends Object") produces true. This should be false IMO since ? in ? extends Number can be bound to Integer and ? in ? extends Object can be bound to String and Number.isAssignmentCompatible(String) == false;
Regarding the case in comment #1. Philippe, I tried your idea: public class A<LHS, RHS> { LHS lhs; RHS rhs; { A<? extends Object, ? extends Number> a= null; a.lhs= a.rhs; } } This produces a compile error saying: Bound mismatch: Cannot assign expression of type ? extends Number to wildcard type ? extends Object. The wildcard type has no lower bound, and may actually be more restrictive than expression type. So "? extends Number".isAssignmentCompatible("? extends Object") should return false then.
Indeed. I cannot reproduce the "true" answers from isAssignmentCompatible; using source code. I suspect our API is buggy, since it isn't the true semantics played by the compiler. Can you provide the right testcase you used ? import java.util.List; public class X<U extends Number> { U u; void foo(X<? extends Number> xn, X<? extends U> xu) { xu.u = xn.u; // ko xn.u = xu.u; // ko } }
Philippe, the test I use is TypeEnvironmentTests in jdt.ui.tests.refactoring. To enable the wildcard assignment tests go to method TypeEnvironmentTests#performGenericAssignmentTest uncomment the line testAssignment. This will print the result of the comparison to the console together with the result the context wide implementation in the UI computes. You need the code from HEAD to test this.
Please reopen if this is still an issue in latest.
reopening for closing properly
closing, since didn't hear back in a long time.