我遇到了这种我不明白的类型不匹配:
error: type mismatch;
found : org.fluentlenium.core.domain.FluentList[_<:org.fluentlenium.core.domain.FluentWebElement]
required: org.fluentlenium.core.domain.FluentList[?0(in value $anonfun)] where type ?0(in value $anonfun) <: org.fluentlenium.core.domain.FluentWebElement
Note: org.fluentlenium.core.domain.FluentWebElement >: ?0, but Java-defined class FluentList is invariant in type E.
You may wish to investigate a wildcard type such as `_ >: ?0`. (SLS 3.2.10)
事实上,精确的“找到”值是以下类型:
org.fluentlenium.core.domain.FluentList[_<:org.fluentlenium.core.domain.FluentWebElement]
=> 变体类型参数
我无法代表这样的情况,其中“找到”值是变体类型参数。我尝试了这个简单的代码段:
public class CarList<E extends Car> implements Collection<E> { // written in Java
//overriden methods from Collection here
}
public class Car{} // written in Java
public Ferrari extends Car{} //written in Java
object Main extends App {
val carList: CarList[Car] = new CarList[Car]
val l: CarList[Ferrari] = carList
}
发生的编译错误非常相似:
error: type mismatch;
found : app.CarList[app.Car] //but in this case, logically it's an invariant type: Car
required: app.CarList[app.Ferrari]
Note: app.Car >: app.Ferrari, but Java-defined class CarList is invariant in type E.
You may wish to investigate a wildcard type such as `_ >: app.Ferrari`. (SLS 3.2.10)
val l: CarList[Ferrari] = carList
^
如何修改我的代码片段以完全结束:
- 与 ' 的错误相同类型的错误
FluentList
(在“找到”值中精确变体类型参数):
found : app.CarList[_ :> app.Car]
- 来自编译器的相同建议:
You may wish to investigate a wildcard type such as _ >:
这样我就可以弄清楚问题的根源是什么?