gpt4 book ai didi

scala - 函数组合导致方法首先缺少参数列表

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

假设我们有以下函数:

def first(a: A): B = ???
def second(b: B): C = ???

我想使用 andThen 组合它们,但是下面的代码:

(first andThen second)(a)

结果:

<console>:14: error: missing argument list for method first
Unapplied methods are only converted to functions when a function type is expected.
You can make this conversion explicit by writing `first _` or `first(_)` instead of `first`.
(first andThen second) (1)

最佳答案

andThen()Function 上的一个方法。正如您定义的那样,first() 是一种方法 (def),而不是函数。这些是 different entities .

你可以将它定义为一个函数...

val first : A => B = (a:A) => ???

... 或者您可以使用 eta expansion将其提升到功能状态。

(first _ andThen second)

据说 Scala 3 将提供更透明的 eta 扩展机制,这样方法和函数之间的区别就不会那么麻烦了。

关于scala - 函数组合导致方法首先缺少参数列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62618108/

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