0
我想要測試泛型類型的向量和輸入參數的列表之間的等式案例類構造函數。我已經想出瞭如何使用反射來獲取參數列表和它們的類型,但是我無法弄清楚當對象實例是通用的時候如何測試對象實例和這些類型之間的相等性。通用通配符的斯卡拉反射類型相等
下面是一個簡單的例子:
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)
我希望能夠測試條的類型是棒型的,但是這是接近我可以得到的。我不知道如何獲得具有指定的泛型值的類型簽名。
scala>runtimeMirror(barClass.getClassLoader).classSymbol(barClass).toType
res112: reflect.runtime.universe.Type = Bar[T]
scala> getType(bar2)
res113: reflect.runtime.universe.Type = Bar[Int]
我對Scala很陌生(和JVM反射一般),所以感謝您的幫助。