简体   繁体   中英

Scala Manifested with Parameterized Types

Is it possible to get the type parameters of a manifest type included in the class object.

I might not be describing that correctly, so what I mean is, given this type.

Resultset[Band[Coldplay]]

I want a manfiest that represents the full type, so that it should be possible to get a class instance that has the type

Class[Resultset[Band[Coldplay]]]

All I can get is

Class[Resultset[_]]

You could build a manifest into a class:

case class Manifesting[A](value: A)(implicit val mf: Manifest[A]) { }

scala> Manifesting(5).mf.erasurescala> Manifesting(5).mf.erasure
res1: Class[_] = int

Or you can build all the manifests into a method:

def nested[A, B[A]](x: B[A])(implicit ma: Manifest[A], mb: Manifest[B[A]]) = 
  (ma.erasure, mb.erasure)

scala> nested(List("fish"))
res2: (Class[_$1], Class[_$1]) forSome { type _$1; type _$1 } =
  (class java.lang.String,class scala.collection.immutable.List)

Or, in Scala 2.10 you can use TypeTag :

def nest2[A: scala.reflect.runtime.universe.TypeTag](x: A) = 
  implicitly[scala.reflect.runtime.universe.TypeTag[A]]

scala> nest2(Right(List("salmon","herring")))
res3: reflect.runtime.universe.TypeTag[scala.util.Right[Nothing,List[String]]] =
  TypeTag[scala.util.Right[Nothing,List[String]]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM