具有多个参数与多个参数列表的组合函数

问题描述 投票:1回答:2

给出以下示例,如果我尝试使用多个参数列表或多个参数来组合一个函数,我会发现它有所作为。我看不出为什么它不一致。

val foo: Int => Int => Int = ???
foo.curried.andThen(???) // KO
foo.tupled.andThen(???) // KO
foo.andThen(???) // OK
val bar: (Int, Int) => Int = ???
bar.curried.andThen(???) // OK
bar.tupled.andThen(???) // OK
bar.andThen(???) // KO

为什么Scala编译器无法将bar作为函数处理,我可以compose /调用andThen

scala function-composition scala-2.12
2个回答
3
投票

无法实现Function2.compose,因为该参数必须是一个返回两个值(传递给Function2.apply的函数)。 Scala语言不支持多个返回值,因此编译器对此无能为力。

实现Function2.andThen是可能的,但是范例通过多个函数链接一个值,并且由于返回值的相同问题,Function2无法实现。


0
投票

确定,我发现了-与scala.Function2相比,andThen没有实现Function1

© www.soinside.com 2019 - 2024. All rights reserved.