1

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

Community
  • 1
  • 1
J Pullar
  • 1,915
  • 2
  • 18
  • 30

1 Answers1

2

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
Leo
  • 757
  • 5
  • 11