陌生感,我碰到這種類型不匹配,我不明白: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