I'm trying to test equality between a vector of generic types and a list of input parameters to a case class constructor. I've figured out how to use reflection to get the list of parameter and their types, but I can't figure out how to test for equality between the object instances and those types, when the object instance is a generic.
Here is a simplified example:
abstract class Foo[T]
case class Bar[T](value: Seq[T]) extends Foo[Seq[T]]
def getTypeTag[T: TypeTag](obj: T) = typeTag[T]
// In my use case I have a function that can return any
// Foo, which is why I've used the wildcarded generic here
val bar: Foo[_] = Bar[Int](Seq(2))
// In my use case I actually get the type for a parameter list
// of a case class constructor, but putting this here for simplicity
val bar2 = Bar(Seq(1))
var barType = getType(bar2)
I want to be able to test that bar is of type barType, but this is as close as I can get. I'm not sure how to get the type signature with the specified generic value.
scala>runtimeMirror(barClass.getClassLoader).classSymbol(barClass).toType
res112: reflect.runtime.universe.Type = Bar[T]
scala> getType(bar2)
res113: reflect.runtime.universe.Type = Bar[Int]
I'm pretty new to Scala (and JVM reflection in general), so thanks for the help .
Aucun commentaire:
Enregistrer un commentaire