Say you have a method definition like this in Scala:
def handle[T](fn: T => Unit): Unit
Is it possible to pattern match on the type of the function parameter T to call up a different method depending on the type of T?
Would you need to rewrite it instead to take a Function1 instead and then pattern match on it?
I've tried the following but does not work due to type erasure:
class A {
def x(fn: A => Unit): Unit = fn(this)
}
class B {
def y(fn: B => Unit): Unit = fn(this)
}
def handle[T](fn: Function1[T, Unit]): Unit = {
fn match {
case fnA: Function1[A, Unit] =>
new A().x(fnA)
case fnB: Function1[B, Unit] =>
new B().y(fnB)
}
}
Maybe with abstract types?
handle? And to changeAandB?