我有以下代码:
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 构造非常慢。所以,我想创建一些宏扩展,以便快速执行这些构造。但我不能提交多个文件。
谢谢!