在定义的同一文件中使用宏有什么技巧吗?
Is there any trick to use macros in the same file they are defined?
我有以下代码:
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()
^
我的问题是:有没有办法让编译器 "fool" 在它们定义的同一个文件中使用宏扩展?我的动机是这样的:我喜欢用Scala写代码,最近我在网上judge提交了一些问题Codeforces,结果发现有些Scala的构造很慢。所以,我想创建一些宏扩展以便快速执行这些构造。但是我不能提交超过一个文件。
谢谢!
目前,这在 Scala 2.10 和 2.11 的生产版本中是不可能的。我们也许可以通过 scala.meta 实现这一目标,但那是未来的事。
我有以下代码:
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()
^
我的问题是:有没有办法让编译器 "fool" 在它们定义的同一个文件中使用宏扩展?我的动机是这样的:我喜欢用Scala写代码,最近我在网上judge提交了一些问题Codeforces,结果发现有些Scala的构造很慢。所以,我想创建一些宏扩展以便快速执行这些构造。但是我不能提交超过一个文件。
谢谢!
目前,这在 Scala 2.10 和 2.11 的生产版本中是不可能的。我们也许可以通过 scala.meta 实现这一目标,但那是未来的事。