如何在scala运行时中获取别名类型的别名?

时间:2017-11-08 21:13:14

标签: scala reflection types scala-reflect scala-2.12

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]。有没有办法从这个(Int, String, Unit, Nothing, Float)中提取完整的TypeTag元组信息?

1 个答案:

答案 0 :(得分:2)

您可以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)