I think you can switch the concept.
PartialFunction[-A, +B] extends (A) β B
However, you cannot use the value of a superclass where a subclass is expected, because the subclass is more specific. Thus, you cannot return the value of Function1 from the method entered to return the PartialFunction .
The converse works, though - you can use a subclass where a superclass is expected . Thus, you can return the PartialFunction from the method dialed to return Function1 (with the same parameters):
scala> def f: Any => Any = { case a => "foo" } f: Any => Any scala> f(1) res0: Any = foo
In this particular case, you can always convert Function to PartialFunction , so the PartialFunction.apply method is PartialFunction.apply .
def apply[A, B](f: A => B): PartialFunction[A, B] = { case x => f(x) }
sourcedelica
source share