【问题标题】:Detect case class in scala macro检测scala宏中的案例类
【发布时间】:2017-06-19 14:28:30
【问题描述】:

在作为 scala (2.11) 宏调用的方法中,有没有办法以编程方式确定 Type 是否为 case class

我正在处理的方法的 API 归结为:

def typeIsCaseClass(c: Context)(targetType: c.universe.Type): Boolean = {
  // targetType "is case class?"
}

如果需要,我愿意更改 API。

【问题讨论】:

    标签: scala reflection scala-macros case-class scala-2.11


    【解决方案1】:

    符号通常包含所有有趣的信息:

    def typeIsCaseClass(c: Context)(targetType: c.universe.Type): Boolean = {
      val sym = targetType.typeSymbol
      sym.isClass && sym.asClass.isCaseClass
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-30
      相关资源
      最近更新 更多