3 ms·
In concrete terms, imagine Foo looks like this: private T value; void test(Foo<T> f) { f.value = this.value; } You could then assign anything to
by salmonellaeater 12y ago
In concrete terms, imagine Foo looks like this:
private T value;
void test(Foo<T> f) {
f.value = this.value;
}
You could then assign anything to a member that should only hold a String.
This is the same reason why arrays should not be covariant (even though they are in Java). This compiles:
Object[] arr = new Integer[2]; // This should be a compile error, but the language spec allows it
arr[0] = "abc"; // This blows up with an ArrayStoreException at runtime
- peeters 12y agoYour example presents zero unsafety for the usage presented by eridal. The only reason it's considered unsafe is because the compiler isn't sophisticated enough to compare captures of wildcards, and in this case, might not be sophisticated enough to know that `f` isn't being reassigned.
- tel 12y agoI understand the covariance bit, but I think I just fail to understand the Java type semantics more broadly. I'd have to just study it and become more familiar