我試圖寫一個簡單的工具斯卡拉通用上限
def withParLevel[T, Col <: ParIterable[T]](coll: Col, ts: TaskSupport): Col = {
coll.tasksupport = ts
coll
}
withParLevel(List(1,2,3,4,5).par, blockingPool)
這給了我這個錯誤:
inferred type arguments [Nothing,scala.collection.parallel.immutable.ParSeq[Int]] do not conform to method withParLevel's type parameter bounds [T,Col <: scala.collection.parallel.ParIterable[T]]
我該如何解決呢?
爲什麼它推斷T爲Nothing,而不是Int?
PS scala版本2.10.2
好奇,我不知道爲什麼類型推斷在這裏失敗。我會等待有人解釋這一點。與此同時,將它改爲通配符('[T <:ParIterable [_]])或視圖綁定('[A,B <%ParIterable [A]]')似乎可行... –
I' d完全刪除'Col'類型參數並使用子類型多形主義:'def withParLevel [T](coll:ParIterable [T]):ParIterable [T] = coll' –