Is there any trick to using macros in the same file that they are defined?

I have the following code:

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() } } 

It throws the following compilation error:

 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() ^ 

My question is: is there a way to β€œtrick” the compiler into using macro extensions in the same file that they are defined? My motivation is this: I like the code in Scala, and lately I asked some problems in the online judge Codeforces and some Scala constructs turned out to be very slow. So, I want to create some macro extensions to quickly complete these builds. But I can not submit more than one file.

Thanks!

+6
source share
1 answer

This is currently not possible in production releases of Scala 2.10 and 2.11. We could achieve this with scala.meta, but this is good in the future.

+5
source

Source: https://habr.com/ru/post/980921/


All Articles