9

我有以下代码:

object Macros {

  import scala.language.experimental.macros
  import scala.reflect.macros.blackbox

  def hello(): Unit = macro hello_impl

  def hello_impl(c: blackbox.Context)(): c.Expr[Unit] = {
    import c.universe._
    reify {
      println("Hello World!")
    }
  }
}


object Main {

  def main(args: Array[String]): Unit = {
    Macros.hello()
  }

}

它会引发以下编译错误:

Error:(21, 17) macro implementation not found: hello
(the most common reason for that is that you cannot use macro implementations in the same compilation run that defines them)
    Macros.hello()
                ^

我的问题是:有没有办法“愚弄”编译器以便在它们定义的同一个文件中使用宏扩展?我的动机如下:我喜欢用 Scala 编写代码,最近我在网上评委Codeforces上提交了一些问题,结果发现一些 Scala 构造非常慢。所以,我想创建一些宏扩展,以便快速执行这些构造。但我不能提交多个文件。

谢谢!

4

1 回答 1

8

目前,这在 Scala 2.10 和 2.11 的生产版本中是不可能的。我们或许可以使用 scala.meta 来实现这一点,但这在未来会很好。

于 2015-01-13T18:35:58.480 回答