mardi 17 mars 2015

Comparing reflected collection types in Scala

I have a reflected collection type I need to compare to see if it's a certain kind of collection. How can I do that?



val a = List(1,2,3)
val b = currentMirror.classSymbol(a.getClass).typeSignature
println("Q? "+ (b =:= typeOf[List[_]]))


This is always false, of course. In practice I have a list of collections, generalized with _ . I need to know if a given Type (typically specific in its parameters) is one of these collections. For example I need to know if List[Int] is a List[_].


The actual case is this: Map of generic type -> function for something I need:



val collectionHandlers = Map(
typeOf[scala.collection.immutable.List[_]] -> fnList,
typeOf[scala.collection.immutable.Map[_,_]] -> fnMap,
//...
)
val aListType = // something here that is a TypeSignature of List[Int] as above
collectionHandlers( _magicClean(aListType) )()


Where _magicClean is what I need help with. How to I "generalize" the specifically-typed collection so that map lookup will work?






Aucun commentaire:

Enregistrer un commentaire