【问题标题】:Is it possible to generate Apply from WeakTypeTag inside a scala macro?是否可以在 scala 宏中从 WeakTypeTag 生成 Apply?
【发布时间】:2013-06-28 07:26:38
【问题描述】:

我的宏中有某种类型的WeakTypeTag,我想生成如下代码:

macroCreate[SomeObject] // => SomeObject(1)

宏的定义是这样的:

def macroCreate[A] = macro _macroCreate[A]
def _macroCreate[A](c: Context)(implicit wtt: c.WeakTypeTag[A]) = {
  c.Expr(Apply(Select(???, newTermName("apply")), List(c.literal(1).tree)))
}

问题是,我如何获得给定类型的Select

我可以使用一种解决方法将类型转换为字符串,在"." 上拆分,然后从字符串列表中创建一个Select,但这似乎很老套。

是否可以直接从类型标签创建Select

【问题讨论】:

    标签: scala macros scala-2.10 scala-macros


    【解决方案1】:

    可以获取伴生对象的符号,然后使用宇宙的Ident(sym: Symbol): Ident工厂方法:

    def macroCreate[A] = macro _macroCreate[A]
    
    def _macroCreate[A](c: Context)(implicit wtt: c.WeakTypeTag[A]) = {
      import c.universe._
    
      c.Expr(
        Apply(
          Select(Ident(wtt.tpe.typeSymbol.companionSymbol), newTermName("apply")),
          c.literal(1).tree :: Nil
        )
      )
    }
    

    然后:

    scala> case class SomeObject(i: Int)
    defined class SomeObject
    
    scala> macroCreate[SomeObject]
    res0: SomeObject = SomeObject(1)
    
    scala> macroCreate[List[Int]]
    res1: List[Int] = List(1)
    

    如果你的意思是 SomeObject 是对象的类型(即,不是它的伴生类的类型),只需删除上面的 .companionSymbol

    【讨论】:

    • 我添加了一个小编辑(选择中的newTermName),因为 scalac 会在其上引发弃用警告。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多