为什么,在Scala中,给出:
a = List(1, 2, 3, 4)
def f(x : String) = { x }
Run Code Online (Sandbox Code Playgroud)
不
a.map(_.toString)
Run Code Online (Sandbox Code Playgroud)
工作,但是
a.map(f(_.toString))
Run Code Online (Sandbox Code Playgroud)
给出错误
missing parameter type for expanded function ((x$1) => x$1.toString)
Run Code Online (Sandbox Code Playgroud) 有人可以解释为什么以下不起作用.当我这样做时toSet,不知何故失去了编译类型推断的一些信息,但我不明白为什么.
scala> case class Foo(id: Int, name: String)
defined class Foo
scala> val ids = List(1,2,3)
ids: List[Int] = List(1, 2, 3)
scala> ids.toSet.map(Foo(_, "bar"))
<console>:11: error: missing parameter type for expanded function ((x$1) => Foo(x$1, "bar"))
ids.toSet.map(Foo(_, "bar"))
^
scala> ids.map(Foo(_, "bar")).toSet
res1: scala.collection.immutable.Set[Foo] = Set(Foo(1,bar), Foo(2,bar), Foo(3,bar))
Run Code Online (Sandbox Code Playgroud) scala ×2