[英]“Unexpected token” using lower-bounded wildcard (Java)
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: 但是IntelliJ正在报道:
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? 或者我只允许在上限使用R&X
表达式?
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; 通过我对规范的阅读, super
只能与通配符一起使用,不能被捕获到类型变量中; see JLS 4.5.1 . 见JLS 4.5.1 。 Similarly, &
is only valid in type variables , not type arguments , and type variables can't use super
. 类似地, &
仅在类型变量中有效,而不是类型参数 ,并且类型变量不能使用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
. 当你声明一个类型参数是super Foo
,你说这个参数可以是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; 这意味着它可以是包括Object
在内的任何东西,因此您没有安全的方法来假设其类型满足该范围的对象的任何内容,因此在命名的类型变量中没有包含任何信息; you just wildcard it and can call hashCode()
or toString()
, but nothing type-specific. 你只是通配符并且可以调用hashCode()
或toString()
,但没有特定于类型的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.