Overriding curried functions in Scala
scala
Solution
The two methods described quite simply have different signatures. The REPL confirms this:
scala> def foo1(a: Int)(b: Int): Int = a + b
foo1: (a: Int)(b: Int)Int
scala> def foo2(a: Int): (Int => Int) = (b: Int) => a + b
foo2: (a: Int)Int => Int
The first is a function that requires two arguments, given in separate argument lists, and returns an `Int`. The second is a function that takes one argument and returns a function from `Int` to `Int`. While these two things are conceptually similar, they are, in fact, different constructs, and Scala treats them as such.
This is not limited to functions with multiple argument lists. It works the same way here:
scala> def foo3(a: Int): Int = a + 1
foo3: (a: Int)Int
scala> def foo4: (Int => Int) = (a: Int) => a + 1
foo4: Int => Int
Note that there are different ramifications for usage as well. With `foo2`, because it only accepts one argument, we can call it with just one argument. However, `foo1` requires two arguments, an so we cannot simply call it with one. You can however use the `_` syntax to convert it into a callable function.
foo2(2) // Int => Int = <function1>
foo1(2) // error: missing arguments for method foo1
foo1(2) _ // Int => Int = <function1>
So to answer your question directly: The reason they are not interchangeable is because they are not the same. If they were the same, we would be able to call them the same way. If you could change the signature upon extension, how would Scala know which calling syntax to allow? The way to "get around" this is to simply make the signatures consistent.
Problem
I was under the impression that this ``` // short syntax def foo(bar: Bar)(baz: Baz): Quux ``` was syntax sugar for this ``` // long syntax def foo(bar: Bar): (Baz) => Quux ``` But I cannot seem to mix the two when it comes to inheritance. The whole tree has to be defined in either the short syntax or the long syntax; never both. For example: ``` case class Context case class Work trait ContextualWorker { def workWithContext(ctxt: Context)(work: Work): Traversable[Work] } class ShortConcreteWorker extends ContextualWorker { override def workWithContext(ctxt: Context)(work: Work) = Nil } class LongConcreteWorker extends ContextualWorker { // error on next line: method workWithContext overrides nothing <------------- override def workWithContext(ctxt: Context): (Work) => Traversable[Work] = { val setupCode = 1 { work => Nil } } } ``` If I change the trait to use the long syntax, then ShortConcreteWorker doesn't compile. Is there a reason why these aren't interchangeable/inheritable? How have you gotten around it? Right now the most flexible approach appears to be to define the tree in the long syntax, perhaps delegating to an implementation class in ShortConcreteWorker like so: ``` case class Context case class Work trait ContextualWorker { def workWithContext(ctxt: Context): (Work) => Traversable[Work] } class ShortConcreteWorker extends ContextualWorker { override def workWithContext(ctxt: Context) = workWithContextImpl(ctxt)_ private def workWithContextImpl(ctxt: Context)(work: Work) = Nil } class LongConcreteWorker extends ContextualWorker { override def workWithContext(ctxt: Context): (Work) => Traversable[Work] = { val setupCode = 1 { work => Nil } } } ```