2015-07-28 189 views
1

使用TypeTag我已经能够检索到关于我的多态类型的类型信息。获取Scala类型的类型参数

scala> paramInfo(List(1,2)).tpe 
res18: reflect.runtime.universe.Type = List[Int] 

现在我想检索Int.type,但显然我无法这样做。

scala> paramInfo(List(1,2)).tpe.typeParams 
res19: List[reflect.runtime.universe.Symbol] = List() 

是我想做的事情,如果是这样,我该怎么做?

回答

1

你需要

paramInfo(List(1,2)).tpe match { 
    case TypeRef(_, _, params) => params 
} 
0

您可以简单地改变的paramInfo签名这样的:

def paramInfo[T](x: List[T])(implicit tag: TypeTag[T]): TypeTag[T] = tag