如何在 Scala 运行时获取类型别名的别名类型?
How to get the aliased type of a type alias in scala runtime?
import scala.reflect.runtime.universe._
object Main {
final type INeedDetails = (Int, String, Unit, Nothing, Float)
def main(args: Array[String]): Unit = println {
typeTag[INeedDetails]
}
}
上面的代码片段将打印 TypeTag[Main.INeedDetails]
。有没有办法从这个TypeTag
中提取完整的(Int, String, Unit, Nothing, Float)
元组信息?
您可以dealias
标签中的类型:
scala> type INeedDetails = (Int, String, Unit, Nothing, Float)
defined type alias INeedDetails
scala> typeTag[INeedDetails].tpe
res1: reflect.runtime.universe.Type = INeedDetails
scala> typeTag[INeedDetails].tpe.dealias
res2: reflect.runtime.universe.Type = (scala.Int, String, scala.Unit, scala.Nothing, scala.Float)
import scala.reflect.runtime.universe._
object Main {
final type INeedDetails = (Int, String, Unit, Nothing, Float)
def main(args: Array[String]): Unit = println {
typeTag[INeedDetails]
}
}
上面的代码片段将打印 TypeTag[Main.INeedDetails]
。有没有办法从这个TypeTag
中提取完整的(Int, String, Unit, Nothing, Float)
元组信息?
您可以dealias
标签中的类型:
scala> type INeedDetails = (Int, String, Unit, Nothing, Float)
defined type alias INeedDetails
scala> typeTag[INeedDetails].tpe
res1: reflect.runtime.universe.Type = INeedDetails
scala> typeTag[INeedDetails].tpe.dealias
res2: reflect.runtime.universe.Type = (scala.Int, String, scala.Unit, scala.Nothing, scala.Float)