Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding curried functions in Scala

Tags:

scala

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 }
  }
}
like image 441
Michael Deardeuff Avatar asked Apr 30 '12 21:04

Michael Deardeuff


People also ask

What is curried function in Scala?

Currying in Scala is simply a technique or a process of transforming a function. This function takes multiple arguments into a function that takes single argument. It is applied widely in multiple functional languages. Syntax. def function name(argument1, argument2) = operation.

What is the advantage of currying in Scala?

Advantages of Currying Function in Scala One benefit is that Scala currying makes creating anonymous functions easier. Scala Currying also makes it easier to pass around a function as a first-class object. You can keep applying parameters when you find them.

What is partially applied function in Scala?

The Partially applied functions are the functions which are not applied on all the arguments defined by the stated function i.e, while invoking a function, we can supply some of the arguments and the left arguments are supplied when required.

Is currying supported in scheme?

It is possible to generate curried functions in Scheme. The function curry2 generates a curried version of a function, which accepts two parameters. The curried version takes one parameter at a time. Similarly, curry3 generates a curried version of a function that takes three parameters.


1 Answers

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.

like image 139
dhg Avatar answered Oct 23 '22 01:10

dhg