Scala - 推断类型参数 [英] Scala - infer type parameters
本文介绍了Scala - 推断类型参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么这段代码会产生错误
Why does this code produce an error
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))
Scala 不应该知道 A 是 Int 吗?
shouldn't Scala be able to tell that A is Int?
推荐答案
您需要第二个参数列表才能工作.我不确定规范中是如何定义的,但我以前见过这个.
You need a second parameter list for this to work. I'm not sure how this is defined in the spec, however I have seen this before.
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)
这篇关于Scala - 推断类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文