I want to use isInstanceOf to decide the type of a variable,
however isInstanceOf[T] requires T and T has to be a class decided in compile time. I hope I can make it a variable.
See the example code:
class A{ ... }
class B{ ... }
val class_map = Map( classOf[A] -> 1, classOf[B] -> 2 )
val a = new A()
class_map.keys foreach { i =>
if (a.isInstanceOf[ i ]) // how to make this statement work?
println(class_map[i])
}
isInstanceOfis usually a code smell, and this looks like an XY Problem, can you describe the broader problem you want yo solve. There may be better alternatives.