簡體   English   中英

Scala中的Iterable Trait方法缺少參數類型錯誤。

[英]Missing parameter type error with Iterable Trait method in Scala.

我可以將類類型存儲在數組中,並從中創建實例。

class A
val keys = Array[Class[_]](classOf[A])
keys(0).newInstance
> res130: Any = A@339319d

但是,當我嘗試使用Iterable Trait方法時,出現參數類型錯誤。

keys.zipWithIndex { case (t, i) => t.newInstance }

error: missing parameter type for expanded function
The argument types of an anonymous function must be fully known. (SLS 8.5)
Expected type was: scala.collection.generic.CanBuildFrom[Array[Class[_]],(?, Int),?]

可能是什么問題?

zipWithIndex除了隱式的CanBuildFrom之外,不帶任何參數,在您的情況下,它將被模式匹配替換。 看起來您正在尋找的是map或其他方法來迭代元素。 嘗試這樣的事情:

  keys.zipWithIndex.map { case (t, i) => t.newInstance }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM