我知道如何用Manifest做到这一点。我想知道如何使用TypeTag或ClassTag来完成它。
仅供参考,通用类型是别名。
case class S()
case class M()
case class MyMultiPurposeClass[T](member: T)
type MyTypeS = MyMultiPurposeClass[S]
type MyTypeM = MyMultiPurposeClass[M]
def create[T](implicit tag: TypeTag[T], mf: Manifest[T]): T = {
// It's this step I want to know how to do it with TypeTag or ClassTag.
val member =
mf.typeArguments.head.runtimeClass.getConstructors.head.newInstance()
// But, of course, the ultimate goal of this function is to create a MyTypeS
// or MyTypeM instance.
ev.runtimeClass.getConstructors.head.newInstance(member)
// Do the same thing with tag.
???
}
create[MyTypeS]
create[MyTypeM]