A macro is a method that is executed during compilation to produce the code of the program.
The Scala 2.13 macro API is closely tied to the Scala 2.13 compiler internals. Therefore it is not possible for the Scala 3 compiler to expand any Scala 2.13 macro.
In contrast, Scala 3 introduce a new principled approach of metaprogramming that is designed for stability. All Scala 3 macros will be compatible with future versions of the Scala 3 compiler. While this is an uncontested improvement, it also means that Scala 2.13 macro implementations have to be rewritten from the ground up.
A Scala 3 module cannot depend on a Scala 2.13 macro
The Scala 3 compiler cannot execute a macro defined in a Scala 2.13 artifact.
But a Scala 3 module can depend on a Scala 2.13 artifact whose compilation has involved a Scala 2.13 macro expansion.
A Scala 2.13 module cannot depend on a Scala 3 macro
The Scala 2.13 compiler cannot execute a macro defined in a Scala 3 artifact.
But a Scala 2.13 module can depend on a Scala 3 artifact whose compilation has involved a Scala 3 macro expansion.
The macro can be defined inside the Scala 3 module that consumes it.
Before Rewriting a Macro
Before getting deep into reimplementing a macro you should check if it can be supported using Scala 3 new features.
- Can I encode the logic of the macro using the new scala 3 features?
- Can I use match types to reimplement the interface of my macro?
- Can I use
inlineand the metaprogramming features in
scala.compiletimeto reimplement my logic?
- Can I use the simpler and safer expression based macros to implement my macro?
- Do I really need to access the Abstract Syntax Trees?
You can learn the new metaprogramming concepts by reading the Macro Tutorial:
Cross-building a Macro Library
You have written a wonderful macro library and you would like it to be available for Scala 2.13 and Scala 3. There are two different approaches, the traditional cross-building technique and the more recent mixing macros technique.
Both approaches are described in the tutorials section:
Blog posts and talks: