【问题标题】:How to get the aliased type of a type alias in scala runtime?如何在scala运行时获取类型别名的别名类型?
【发布时间】:2018-04-21 17:39:27
【问题描述】:
import scala.reflect.runtime.universe._

object Main {

  final type INeedDetails = (Int, String, Unit, Nothing, Float)

  def main(args: Array[String]): Unit = println {
    typeTag[INeedDetails]
  }

}

上面的 sn-p 将打印TypeTag[Main.INeedDetails]。有没有办法从这个TypeTag 中提取完整的(Int, String, Unit, Nothing, Float) 元组信息?

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多