【问题标题】:How do I get type parameter from type with type parameter, inside scala macro?如何在 scala 宏中从带有类型参数的类型中获取类型参数?
【发布时间】:2013-06-14 13:14:14
【问题描述】:

我有一个类型(在这种情况下,它表示方法的返回类型),它的格式为List[Int](例如)。

我知道该类型携带该信息,因为toString 会产生正确的值,但我如何提取该类型?我试过.typeSymbol,但它完全丢失了所有类型信息。

【问题讨论】:

    标签: scala macros scala-2.10


    【解决方案1】:

    您可以使用TypeRef 提取器:

    import reflect.runtime.universe._
    // or in a macro "c.universe._"
    
    val tpe = weakTypeOf[List[Int]]
    
    // extract type parameters
    val TypeRef(_,_, tps) = tpe
    
    // tps has type List[Type]
    println(tps.head) // Int
    

    【讨论】:

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