2

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[_]]

1 Answer 1

4

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]]]
Sign up to request clarification or add additional context in comments.

3 Comments

In your first example, you are losing the nested types? That is the effect I am trying to get around. I can get at them from erasure.typeargs but I want a class instance that has the full reified type, if that's possible.
@monkjack - That is what TypeTag is for in 2.10. In earlier versions, you may be able to build something yourself (using the pieces that I showed), but it's not trivial.
Ah ok, I had a feeling I would need 2.10. Well I guess I'll get around it myself using the old fashioned get the type parameters on construction trick and assign to a field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.