Scala,将函数参数传递给隐式转换类时的编译器错误

时间:2014-12-21 16:37:57

标签: scala

class RichCollection[T](underlying: List[T]) {
  def zip[A](fn: T => A): List[(T,A)] = underlying.map(i => (i, fn(i)))
}
implicit def collectionToRichCollection[T](collection: List[T]) = new RichCollection(collection)

val list = List("a", "bb", "ccc")
def fn(str: String) = str.length
list.zip(fn)

这最后一行失败了:

missing arguments for method fn
follow this method with `_' if you want to treat it as a partially applied function

它实际上是通过修改编译器提出的代码来实现的:

list.zip(fn(_))

我的问题就是为什么我需要在那里使用(_),以及为什么在这两种情况下没有它的情况下它会起作用:

collectionToRichCollection(list).zip(fn)
new RichCollection(list).zip(fn)

0 个答案:

没有答案