如何在scala运行时获取类型别名的别名类型? [英] How to get the aliased type of a type alias in scala runtime?

查看:58
本文介绍了如何在scala运行时获取类型别名的别名类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)元组信息?

The snippet above will prnint TypeTag[Main.INeedDetails]. Is there any way to extract the complete (Int, String, Unit, Nothing, Float) tuple information from this TypeTag?

推荐答案

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

这篇关于如何在scala运行时获取类型别名的别名类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆