Search code examples
scalapattern-matchingcasecatch-all

How do you determine the type of the catch all in a scala pattern match case?


If you have a pattern matching (case) in Scala, for example:

foo match {
  case a: String => doSomething(a)
  case f: Float =>  doSomethingElse(f)
  case _ => ? // How does one determine what this was?
}

Is there a way to determine what type was actually caught in the catch-all?


Solution

  • foo match {
      case a: String => doSomething(a)
      case f: Float =>  doSomethingElse(f)
      case x => println(x.getClass)
    }