【发布时间】:2013-12-22 03:59:59
【问题描述】:
我听说在 Scala 中我可以在匿名函数中使用 _ 作为“匿名参数”:
List(1,2,3).foreach(print(_))
但是这段代码无法编译:
scala> def a[T](s: Seq[T]): Seq[T] = s.map(_)
<console>:7: error: missing parameter type for expanded function ((x$1) => s.map(x$1))
这可以:
scala> def a[T](s: Seq[T]): Seq[T] = s.map(x => x)
a: [T](s: Seq[T])Seq[T]
这似乎与类型推断有关。但是x => x 怎么能提供比_ 更多的信息呢?
【问题讨论】:
标签: scala type-inference