2
votes

the following code should typecheck afaik, but it doesn't. I'd rather avoid giving names to the argument types as those can change in arity and type.

trait Foobar[K] {
     def method: K => Double
}
class Test extends Foobar[(String, Int, Boolean)] {
     override def method: (String, Int, Boolean) => Double = (_, _, _) => 3.0
      // This also fails
     // override def method: (String, Int, Boolean) => Double = { case (_, _, _) => 3.0 }
}

The error is

overriding method method in trait Foobar of type => ((String, Int, Boolean) => Double; method method has incompatible type override def method: (String, Int, Boolean) => Double = (,,_) => 3.0

1

1 Answers

3
votes

This is a very trivial but very tedious problem.

(String, Int, Boolean) => Double. is a function of three arguments to a double.
But you want a function of one argument (a tuple of three elements) to a double.

Try with:

class Test extends Foobar[(String, Int, Boolean)] {
  override def method: ((String, Int, Boolean)) => Double = {
    case (_, _, _) => 3.0
  }
}