I want to check the type of method parameters, but I do not know how to do this. See my code:
class X { def x(a: Int, b: String) {} } val methods = classOf[X].getDeclaredMethods methods map { m => m.getParameterTypes.toList map { t => println(t.getName)
Pay attention to the comment in the code. I do not know how to check types in a scala way.
I tried:
t match { case _:String => println("### is a string") case _:Int => println("### is an int") case _ => println("### ?") }
But it cannot be compiled.
I can use the java path to check:
if (t.isAssignableFrom(classOf[String])) // do something else if(t.isAssignableFrom(classOf[Int])) // do something else {}
It seems we should use it in scala, right?
UPDATE:
If I want to use match , I have to write like this:
t match { case i if i.isAssignableFrom(classOf[Int]) => println("### is an Int") case s if s.isAssignableFrom(classOf[String]) => println("### is a String") case _ => println("###?") }
Is this the best answer?
types scala
Freewind
source share