【问题标题】:Missing parameter type error with Iterable Trait method in Scala.Scala 中的 Iterable Trait 方法缺少参数类型错误。
【发布时间】:2014-10-09 19:04:46
【问题描述】:

我可以将类类型存储在一个数组中并从中创建实例。

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),?]

可能出了什么问题?

【问题讨论】:

    标签: scala iterable


    【解决方案1】:

    zipWithIndex 不接受参数,除了隐式CanBuildFrom,在您的情况下,它被替换为您的模式匹配。看起来您正在寻找的是 map 或其他一些迭代元素的方法。试试这样的:

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

    【讨论】:

    • 或者在这种情况下等效,因为索引被简单地丢弃:keys.map(_.newInstance)
    猜你喜欢
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    • 1970-01-01
    • 2018-03-28
    • 1970-01-01
    • 2011-11-29
    相关资源
    最近更新 更多