scala> def pfEmpty[A, B] = new PartialFunction[A, B] { | def apply(a: A): B = sys.error("Not supported") | def isDefinedAt(a: A) = false | } pfEmpty: [A, B]=> java.lang.Object with PartialFunction[A,B] scala> val f = pfEmpty[String, String] f: java.lang.Object with PartialFunction[String,String] = <function1> scala> f.lift res26: (String) => Option[String] = <function1> scala> res26("Hola") res27: Option[String] = None
As @didierd points out in the comments, due to variances of arguments, one instance can cover all possible types of arguments.
scala> object Undefined extends PartialFunction[Any, Nothing] { | def isDefinedAt(a: Any) = false | def apply(a: Any): Nothing = sys.error("undefined") | } defined module Undefined scala> val f: PartialFunction[String, String] = Undefined f: PartialFunction[String,String] = <function1> scala> f.lift apply "Hola" res29: Option[String] = None
missingfaktor Aug 25 '11 at 10:50 2011-08-25 10:50
source share