22

Using type tags, I'm able to see the parameters of some type:

scala> import scala.reflect.runtime.universe._
import scala.reflect.runtime.universe._

scala> typeOf[List[Int]]
res0: reflect.runtime.universe.Type = List[Int]

But I just can't quite figure out how to programmatically get that "Int" out of there, in a general way.

(I've been wandering around in REPL for an hour now, trying permutations on Type, to see what I can obtain from it... I get a lot of things which indicate this is a "List", but good luck on finding that "Int"! And I don't really want to resort to parsing the toString() output...)

Daniel Sobral has an excellent (as usual) quick overview here, in which he gets tantalizingly close to what I'm looking for, but (apparently) only if you happen to know, for that particular class, some specific method whose type can be interrogated:

scala> res0.member(newTermName("head"))
res1: reflect.runtime.universe.Symbol = method head

scala> res1.typeSignatureIn(res0)
res2: reflect.runtime.universe.Type = => Int

But I'm hoping for something more general, which doesn't involve rooting around in the list of declared methods and hoping that one of them will capture (and thus divulge) the tag's current type information somewhere.

If Scala can so easily print "List[Int]", why on earth is it so hard to discover that "Int" part of that -- without resorting to string pattern matching? Or am I just missing something really, really obvious?

scala> res0.typeSymbol.asInstanceOf[ClassSymbol].typeParams
res12: List[reflect.runtime.universe.Symbol] = List(type A)

scala> res12.head.typeSignatureIn(res0)
res13: reflect.runtime.universe.Type = 

Grr...

Tim
  • 1,615
  • 14
  • 17
  • Here's a non-answer: with M7 at least you can get the type arguments by casting to an internal API: `typeOf[List[Int]].asInstanceOf[scala.reflect.internal.Types$TypeApiImpl].typeArguments`. – Travis Brown Oct 11 '12 at 16:03

2 Answers2

16

Starting with Scala 2.11, you can simply use:

yourGenericType.typeArgs.head

See the macros changelog point number 14.

juanmirocks
  • 4,786
  • 5
  • 46
  • 46
14

Sadly, I don't think that there's a method that will give you the parameters, but you can get hold of them this way:

Welcome to Scala version 2.10.0-20121007-145615-65a321c63e (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_35).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import scala.reflect.runtime.universe._
import scala.reflect.runtime.universe._

scala> typeOf[List[Int]]
res0: reflect.runtime.universe.Type = scala.List[Int]

scala> res0 match { case TypeRef(_, _, args) => args }
res1: List[reflect.runtime.universe.Type] = List(Int)

scala> res1.head
res2: reflect.runtime.universe.Type = Int

Edit Here's a slightly nicer way to achieve the same thing (following a discussion on scala-internals):

scala> res0.asInstanceOf[TypeRefApi].args
res1: List[reflect.runtime.universe.Type] = List(Int)
Paul Butcher
  • 10,722
  • 3
  • 40
  • 44
  • This was discussed on the scala-internals mailing list here: https://groups.google.com/forum/#!msg/scala-internals/R1iZXfotqds/zqq8QjMJj74J – Paul Butcher Oct 11 '12 at 16:02
  • That appears to do it! -- though (referencing your link) I share your unease as well! I'd point to my own experience here as an argument as to why this violates the principle of least astonishment. – Tim Oct 11 '12 at 16:05
  • Thanks for the update! Honestly, if I had just run into the documentation somewhere ("Hey, use extractors...") -- maybe in the (currently empty) documentation on Universe, I would have understood right away. But I didn't realize you had to use extractors to pull everything out. (Normally, there's a field for every extractor... and we used-to-be Java people don't always remember that possibility.) Google didn't reveal anything useful, either. – Tim Oct 11 '12 at 23:49
  • 1
    For this specific case, I prefer writing it that way: 'val TypeRef(_, _, tpe :: Nil) = typeOf[List[Int]]' – Alois Cochard Jan 12 '13 at 11:05
  • Can you please tell me how you are importing TypeRefApi and/or TypeRef? I can't get it to compile no matter what I try to import. – Ryan Apr 02 '14 at 05:24
  • Update: I fixed the import issue (had a different import from another example), but now I get a cast error. The problem is that I am using member.typeSignature (ie trying this on a member found at runtime rather than a class known at compile time), which while it also returns runtime.universe.Type, seems to be a different implementation that can't be cast to TypeRefApi. So I'm still stuck. – Ryan Apr 02 '14 at 05:38
  • Update: I answered my own question at http://stackoverflow.com/questions/22802541/how-do-i-get-the-declared-generic-parameter-of-the-type-of-a-member-found-at-r – Ryan Apr 02 '14 at 11:36