给出以下示例,如果我尝试使用多个参数列表或多个参数来组合一个函数,我会发现它有所作为。我看不出为什么它不一致。
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
?
无法实现Function2.compose
,因为该参数必须是一个返回两个值(传递给Function2.apply
的函数)。 Scala语言不支持多个返回值,因此编译器对此无能为力。
实现Function2.andThen
是可能的,但是范例通过多个函数链接一个值,并且由于返回值的相同问题,Function2
无法实现。
确定,我发现了-与scala.Function2
相比,andThen
没有实现Function1