“宏实现参考的形状错误"在 Scala 文档示例中 [英] "macro implementation reference has wrong shape" in the Scala Documentation examples

查看:86
本文介绍了“宏实现参考的形状错误"在 Scala 文档示例中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下宏粘贴自 http://docs.scala-lang.org/overviews/quasiquotes/usecases.html:

import reflect.macros.Context
import language.experimental.macros
val universe = reflect.runtime.universe; import universe._
import reflect.runtime.currentMirror
import tools.reflect.ToolBox
val toolbox = currentMirror.mkToolBox()

object debug {
  def apply[T](x: =>T): T = macro impl
  def impl(c: Context)(x: c.Tree) = { import c.universe._
    val q"..$stats" = x
    val loggedStats = stats.flatMap { stat =>
      val msg = "executing " + showCode(stat)
      List(q"println($msg)", stat)
    }
    q"..$loggedStats"
  }
}

它在 Scala 2.11.1 中产生此错误消息:

It produces this error message in Scala 2.11.1:

Q.scala:9: error: macro implementation reference has wrong shape. required:
macro [<static object>].<method name>[[<type args>]] or
macro [<macro bundle>].<method name>[[<type args>]]
  def apply[T](x: =>T): T = macro impl
                                  ^

我尝试以多种方式改变代码(import reflect.macros.whitebox.Contextimport reflect.macros.blackbox.Context,将 scala. 在每次导入开始时,使参数一致按名称或一致按值,macro impl[T],去掉类型参数,macrodebug.impl,将 apply 放在 impl 之后,等等)没有成功.我究竟做错了什么?它是进口的东西吗?那些(大部分)来自不同的网页,http://docs.scala-lang.org/overviews/quasiquotes/setup.html.

I've tried varying the code in numerous ways (import reflect.macros.whitebox.Context, import reflect.macros.blackbox.Context, putting scala. at the start of each import, making the arguments consistently by-name or consistently by-value, macro impl[T], getting rid of the type parameter, macro debug.impl, putting the apply after the impl, and more) with no success. What am I doing wrong? Is it something in the imports? Those come (mostly) from a different web page, http://docs.scala-lang.org/overviews/quasiquotes/setup.html.

该页面的其他两个示例宏都发生了相同的错误:

The same error occurs on both of the other example macros from that page:

object Macro {
  def apply(x: Int): Int = macro impl
  def impl(c: Context)(x: c.Expr[Int]): c.Expr[Int] = { import c.universe._
    c.Expr(q"$x + 1")
  }
}

object Macro {
  def apply(x: Int): Int = macro impl
  def impl(c: Context)(x: c.Tree) = { import c.universe._
    q"$x + 1"
  }
}

推荐答案

scala Foo.scala 将您提供给它的代码包装在一个匿名类中.结果,类似于:

scala Foo.scala wraps the code that you feed to it in an anonymous class. As a result, something like:

import scala.reflect.macros.Context
import scala.language.experimental.macros

object debug {
  def apply[T](x: T): T = macro impl
  def impl(c: Context)(x: c.Tree): c.Tree = ???
}

变成:

[[syntax trees at end of                    parser]] // Test.scala
package <empty> {
  object Main extends scala.AnyRef {
    def <init>() = {
      super.<init>();
      ()
    };
    def main(args: Array[String]): scala.Unit = {
      final class $anon extends scala.AnyRef {
        def <init>() = {
          super.<init>();
          ()
        };
        import scala.reflect.macros.Context;
        import scala.language.experimental.macros;
        object debug extends scala.AnyRef {
          def <init>() = {
            super.<init>();
            ()
          };
          <macro> def apply[T](x: T): T = impl;
          def impl(c: Context)(x: c.Tree): c.Tree = $qmark$qmark$qmark
        }
      };
      new $anon()
    }
  }
}

但是,宏实现必须在静态对象或包中定义,这就是错误消息试图说明的内容:

However, macro implementations must be defined in static object or bundles, which is what the error messages tries to say:

/Users/xeno_by/Projects/211x/sandbox/Test.scala:5: error: macro implementation reference has wrong shape. required:
macro [<static object>].<method name>[[<type args>]] or
macro [<macro bundle>].<method name>[[<type args>]]
  def apply[T](x: T): T = macro impl
                                ^
one error found

不幸的是,如果将某些东西放入匿名类中,它就不再是静态的,这意味着 scala Foo.scala 风格的开发与宏不兼容.考虑使用替代品,例如scalacsbt.

Unfortunately, if something is put into inside an anonymous class, it's no longer static, which means that scala Foo.scala-style development is incompatible with macros. Consider using alternatives, e.g. scalac or sbt.

这篇关于“宏实现参考的形状错误"在 Scala 文档示例中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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