Open GoogleCodeExporter opened 9 years ago
This proposal should be equally applied to generic types, where it would be
beneficial to allow:
List<String> <: @Readonly List<Object>
Original comment by wdi...@gmail.com
on 14 May 2014 at 11:44
The workaround I'm currently using:
/*>>> import org.checkerframework.checker.nullness.qual.Nullable; */
abstract class ReadOnlyArrayVariance
{
<T> void f1(String[] x)
{
print1(x); // <-- no error
print2(x); // <-- error
}
abstract <T extends /*@Nullable*/String> void print1(T[] y);
abstract void print2(/*@Nullable*/String[] y);
}
It seems like having @Readonly trigger covariance in general seems slightly
type unsafe. For example, take the following incomplete List interface:
interface List<T> {
void append(T element); // <-- mutates, contravariant
T get(int index); // <-- read-only, covariant
int indexOf(T element); // <-- read-only, contravariant
}
Though read-only often implies covariance, it doesn't always, as is the case
for "indexOf".
Strangely, the real java.util.List doesn't use the type parameter in indexOf:
int indexOf(Object element);
This seems to be generally the case for all Java collections types, though I
haven't done an exhaustive search. I don't know why they chose to do this.
IntelliJ's analysis will actually assume that it's actually supposed to be "T"
and warn you when you're not doing it right.
Original comment by kan...@cakoose.com
on 22 May 2014 at 4:37
Original issue reported on code.google.com by
michael.ernst@gmail.com
on 31 Dec 2013 at 4:01