Empty partial function in Scala
Map is a PartialFunction so you can do:
val undefined: PartialFunction[Any, Nothing] = Map.empty
Since Scala 2.10 you can use:
val emptyPf = PartialFunction.empty[String, String]
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 said in the comments, due to argument variances, a single instance can cover all possible argument types.
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