聽說在斯卡拉我可以使用_
在匿名函數 「匿名參數」:爲什麼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
怎麼能提供比_
更多的信息?
+1對佔位符語法的直觀解釋。 –