double(i._1)-6ren">
gpt4 book ai didi

使用 "_"的 Scala 函数映射

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

为什么(在 Scala REPL 中)我可以写,例如,

def double(d: Int) = 2*d
(0 until 10).zipWithIndex.map(i => double(i._1))

或者只是

(0 until 10).zipWithIndex.map(_._1)

我不会写

(0 until 10).zipWithIndex.map(double(_._1))
error: missing parameter type for expanded function ((x$1) => x$1._1) (0 until 10).zipWithIndex.map(double(_._1))

?

最佳答案

Scala 尝试扩展 _._1 inside double。所以,它认为你想要拥有

(0 until 10).zipWithIndex.map(double(i => i._1))

然而,它也看到 i => i._1 并不真正适合 double 的参数类型之一,所以它提示并要求您提供帮助编译器的类型提示。但是,在这种情况下,不可能有正确的类型定义,因此错误消息有点错误。

关于使用 "_"的 Scala 函数映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6655448/

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