Question

Now From Finding type parameters via reflection in Scala 2.10? , I can see how to get type arguments, however I would like to get them from a subclassed parameter type. For example I have

trait ICommand
trait IHandle[T <: ICommand] {
  def handle(t:T):Unit
}

case class MyCommand(i:Int) extends ICommand
trait MyHandler extends IHandle[MyCommand]

So I want to find the ICommand parameter of MyHandler. So far I can get

val t = typeOf[MyHandler]
val s = tt.typeSymbol.typeSignature

Where s.toString will give IHandle[MyCommand], however I would like to pull out MyCommand as a symbol

Was it helpful?

Solution

There may be a shorter way, but this works.

scala> typeOf[MyHandler].baseType(typeOf[IHandle[_]].typeSymbol) match { case TypeRef(_, _, List(tpe)) => tpe.typeSymbol }
res8: reflect.runtime.universe.Symbol = class MyCommand
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top