是否有任何技巧可以在定义的同一文件中使用宏?

时间:2015-01-13 10:16:49

标签: scala scala-macros scala-2.11

我有以下代码:

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结构变得很慢。所以,我想创建一些宏扩展,以便快速执行这些构造。但是我不能提交多个文件。

谢谢!

1 个答案:

答案 0 :(得分:5)

目前,这在Scala 2.10和2.11的生产版本中是不可能的。我们或许可以通过scala.meta实现这一目标,但未来情况良好。

相关问题