繁体   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