Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to extract the item type from a Manifest[List[X]] in Scala?

Tags:

scala

I know something is a List[_] based on the Manifest I've passed into a method, but I need to know what kind of item the list is. Is that information stored in the Manifest somewhere and can you get it out? If not, any suggestions about how to work around the issue?

(Basically, I have a Map[Manifest[_], Blah], where Blah handles cases based on the class type. Handling List[X] is composable based on X, but I need to be able to figure out what X is so I can grab its Blah value out of the map.)

Thanks!

like image 951
Todd O'Bryan Avatar asked Dec 16 '22 02:12

Todd O'Bryan


1 Answers

I think you're looking for typeArguments

scala> manifest[List[Int]]
res1: Manifest[List[Int]] = scala.collection.immutable.List[Int]

scala> res1.typeArguments
res2: List[scala.reflect.Manifest[_]] = List(Int)

scala> res2.head
res3: scala.reflect.Manifest[_] = Int

scala> res3.erasure
res4: java.lang.Class[_] = int
like image 109
Paolo Falabella Avatar answered Feb 15 '23 10:02

Paolo Falabella