Search code examples
scalapartialfunction

Adding new arguments to a partial function


Is there some simple way how to add new arguments to a partial function, so that resulting function is defined in the same domain as before (new arguments have no influence on its partiality)? Following code works, but seems a bit verbose.

  val func : PartialFunction[A, B] = ....

  val f = new PartialFunction[(A,C), B] {
    def isDefinedAt(x: (A,C)): Boolean = func.isDefinedAt(x._1)
    def apply(x: (A,C)):B = func(x._1)
  }

Solution

  • You can do this:

    val f : PartialFunction[(A, C), B] = { case (a, _) if func.isDefinedAt(a) => func(a) }