mercredi 21 mars 2018

How to create an instance of inner generic type of an alias type with TypeTag or ClassTag?

I know how to do it with Manifest. I'm wondering how I can do it with TypeTag or ClassTag.

Just FYI, the generic type is aliased.

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 = {

    val member =
        mf.typeArguments.head.runtimeClass.getConstructors.head.newInstance()
    ev.runtimeClass.getConstructors.head.newInstance(member)

    // Do the same thing with tag.
    ???
}

create[MyTypeS]
create[MyTypeM]





Aucun commentaire:

Enregistrer un commentaire