陌生感,我碰到这种类型不匹配,我不明白:Scala和泛型
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 _ >:
,这样我可以找出可能是问题的由来?
试过简单的'新FluentList [FluentWebElement]'?介意粘贴FluentList构造函数签名? – pedrofurla 2013-02-20 12:37:31
@pedrofurla是的,当然它会解决问题,但我试图理解像这样的问题的真正原因:)'FluentList'是用Java编写的,构造函数的签名是:'FluentList(java.util。收藏 listFiltered)' –
Mik378
2013-02-20 12:42:15
我明白了,您的疑问实际是关于类型差异。 – pedrofurla 2013-02-20 14:07:30