vendredi 15 février 2019

Case Switch Statement with Class Object in Scala

I'm trying to use a case switch statement in Scala to check what class/type a Java Class object represents. I can't pass the actual object, but I can get the class and I need to perform different logic depending on that class object. For example,

def foo(classObj: Class[_]): Any = {
  classObj match {
    case map: Map[String, String] => doMapThings()
    case str: String => doStringThings()
  }
}

However, this doesn't really work because the case statement is looking at the type of Class, which is Class, and will never be Map or String. How can I get the type/class that classObj represents and match on that instead?





Aucun commentaire:

Enregistrer un commentaire