如何使用TypeTag或ClassTag创建别名类型的内部泛型类型的实例?

时间:2018-03-22 00:30:01

标签: scala generics reflection nested-generics

我知道如何用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]

0 个答案:

没有答案