Two ways to do this:
The first method assumes that Xand Yare known types, not general types. Then you can simply do the following:
def fp: PartialFunction[Any,Unit] ={
case x: X => println("got x")
case y: Y => println("got y")
case _ => println("Any")
}
and it will work very well without doing much finalism.
Secondly, use a class like:
def fp[T](t: T)(implicit f: Caller[T]) = f(t)
, , , :
trait Caller[T]{
def apply(t: T)
}
, Caller[Any], . :
object Wrap{
implicit val xCal = new Caller[X]{
def apply(x: X){ println("x called") }
}
implicit val yCal = new Caller[Y]{
def apply(x: Y){ println("y called") }
}
}
object `package`{
implicit val anyCal = new Caller[Any]{
def apply(t: Any){ println("any") }
}
}