gpt4 book ai didi

Scala - 推断类型参数

转载 作者:行者123 更新时间:2023-12-01 10:03:08 24 4
gpt4 key购买 nike

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

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 吗?

最佳答案

您需要第二个参数列表才能工作。我不确定规范中是如何定义的,但是我以前见过这个。

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 - 推断类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13868085/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com