Search code examples
scalareflectionscala-reflect

Dealiasing Types in Scala reflection


How can I resolve aliases given a Type? I.e.

import reflect.runtime.universe._

type Alias[A] = Option[Option[A]]
val tpe = typeOf[Alias[_]] 
val ExistentialType(quantified, underlying) = tpe

How do I get Option[Option[_$1]] from underlying (or from tpe)? I know that typeSymbol does resolve aliases, but it seems to lose the parameters in the process:

scala> val tT = typeOf[Alias[_]].typeSymbol
tT: reflect.runtime.universe.Symbol = class Option

scala> tT.asType.toType
res3: reflect.runtime.universe.Type = Option[A]

scala> tT.asType.typeParams
res4: List[reflect.runtime.universe.Symbol] = List(type A)

Solution

  • The method turns out to be called normalize in 2.10 (deprecated and dealias added in 2.11). Don't know how I managed to miss it during my first search.