【发布时间】:2013-02-21 06:26:20
【问题描述】:
我遇到了这种我不理解的类型不匹配:
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] => 变体类型参数
我无法表示这样的情况,其中“找到”值是变体类型参数。我尝试了这个简单的 sn-p 代码:
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
^
如何修改我的代码 sn-p 以准确地结束:
- 与
FluentList的错误类型相同(精确到“找到”值中的变体类型参数):found : app.CarList[_ :> app.Car] - 来自编译器的相同建议:
You may wish to investigate a wildcard type such as _ >:
这样我就可以弄清楚问题的根源是什么?
【问题讨论】:
-
简单地尝试过
new FluentList[FluentWebElement]?介意粘贴 FluentList 构造函数签名吗? -
@pedrofurla 是的,当然它会解决这个问题,但我试图了解这样一个问题的真正原因:)
FluentList是用 Java 编写的,构造函数的签名是:FluentList(java.util.Collection<E> listFiltered) -
我明白了,您对类型差异的怀疑是真实的。
-
@pedrofurla 是的,确实
-
@pedrofurla 我更新了我的代码 sn-p。