Assuming these imports and definitions (for a self-contained example):
import scala.reflect.runtime.universe._
trait Foo[A, B, C]
object Bar extends Foo[Int, String, Option[Short]]
Explicitly referring to Bar.type
:
internal
.thisType(typeOf[Bar.type].typeSymbol.asClass)
.baseType(typeOf[Foo[_,_,_]].typeSymbol.asClass)
.typeArgs
Without referring to Bar.type
:
val a: Any = Bar // pretend that it's some instance, not necessarily `Bar`
val m = runtimeMirror(getClass.getClassLoader)
internal
.thisType(m.reflect(a).symbol.asClass)
.baseType(typeOf[Foo[_,_,_]].typeSymbol.asClass)
.typeArgs
Both gives:
List(
scala.Int,
String,
scala.Option[scala.Short]
)
Both solutions inspired by this answer here.