scalafunction-compositionscala-2.12

Composing functions with multiple arguments vs multiple argument lists


Given the following example, I can see that it makes a difference, if I try to compose a function with multiple argument lists or just multiple arguments. I fail to see why it's not consistent.

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

Why is the Scala compiler not able to handle bar as a function, which I can compose/ call andThen on?


Solution

  • It is not possible to implement Function2.compose because the argument would have to be a function that returns two values (to pass on to Function2.apply). The Scala language does not support multiple return values so there is nothing that the compiler can do about it.

    Implementing Function2.andThen would be possible, but the paradigm is chaining a value through multiple functions and that is not possible with Function2 because of the same issue with return values.