mardi 6 janvier 2015

Possible to find parameter type of class in Scala where parameters type is a primitive type?

Suppose I have:



class X
{
val listPrimitive: List[Int] = null
val listX: List[X] = null
}


and I print out the return types of each method in Scala as follows:



classOf[ComplexType].getMethods().foreach { m => println(s"${m.getName}: ${m.getGenericReturnType()}") }

listPrimitive: scala.collection.immutable.List<Object>
listX: scala.collection.immutable.List<X>


So... I can determine that the listX's element type is X, but is there any way to determine via reflection that listPrimitive's element type is actually java.lang.Integer? ...



val list:List[Int] = List[Int](123);
val listErased:List[_] = list;
println(s"${listErased(0).getClass()}") // java.lang.Integer


NB. This seems not to be an issue due to JVM type erasure since I can find the types parameter of List. It looks like the scala compiler throws away this type information IFF the parameter type is java.lang.[numbers] .






Aucun commentaire:

Enregistrer un commentaire