I have something along the lines of:
interface Foo<T> {
//... lines [0,45]...
/*line 46*/ <R, X super T&R> List<X> weave(R value);
//...
}
But IntelliJ is reporting:
What's the problem? Am I not allowed to bind a name to a lower bound? Or am I only allowed to use a R&X
expression in an upper bound?
Changing it to
interface Foo<T> {
//... lines [0,45]...
/*line 46*/ <R> List<? super T&R> weave(R value);
//...
}
yields
By my reading of the specification, super
can only be used with a wildcard and can't be captured into a type variable; see JLS 4.5.1. Similarly, &
is only valid in type variables, not type arguments, and type variables can't use super
.
After having thought about it, here's my explanation: The reason for a type variable is to eliminate explicit casting to improve type safety. When you declare a type parameter to be super Foo
, you're saying that it's okay for that parameter to be any superclass of Foo
. This means that it could be anything up to and including Object
, and so you have no safe way to presume anything about the objects whose type satisfies that bound, and so there's no information whatsoever contained within a named type variable; you just wildcard it and can call hashCode()
or toString()
, but nothing type-specific.
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