Scala - 推断类型参数

Spi*_*Pig 3 scala

为什么这段代码会产生错误

def test[A](a: List[A], f: A => A) = a.map(f)

println(test(List(1,2,3), _*2))

error: missing parameter type for expanded function ((x$2) => x$2.$times(2))
Run Code Online (Sandbox Code Playgroud)

Scala不应该告诉A是Int吗?

Iva*_*ith 6

您需要第二个参数列表才能生效.我不确定这是如何在规范中定义的,但我之前已经看过了.

scala> def test[A](a: List[A])(f: A => A) = a.map(f)
test: [A](a: List[A])(f: (A) => A)List[A]

scala> test(List(1))(_+1)
res1: List[Int] = List(2)
Run Code Online (Sandbox Code Playgroud)