0
votes

This is related to Finding type parameters via reflection in Scala 2.10? but the solution given there doesn't work for me. What I'm trying to do is iterate over the members of a class, some of which are lists etc, eg val myField: List[String]. I want to be able to get "String" out of there somehow without having to hack the .toString value of the typeSignature field.

I tried

typeOf[member.typeSignature].asInstanceOf[TypeRefApi].args

but apparently the scala.runtime.Type implementation returned by typeSignature is different from the one returned by typeOf (as used in the above question) because I get the following exception:

java.lang.ClassCastException: scala.reflect.internal.Types$NullaryMethodType cannot be cast to scala.reflect.api.Types$TypeRefApi

I also tried:

val TypeRef(_, _, tpe :: Nil) = member.typeSignature

But then I get this exception:

scala.MatchError: => scala.Option[String] (of class scala.reflect.internal.Types$NullaryMethodType)

Does anyone know how to do this?

1

1 Answers

0
votes

Update: I figured out a way to do this:

val NullaryMethodType(tpe) = member.typeSignature
val TypeRef(_, _, tpe2 :: Nil) = tpe

Then tpe2 will have the correct value. Right now this crashes on non-generic inputs but I can fix that.