Metaprogramming in Scala 3
The Scala 2.13 macros are compiler-dependent by design. A macro compiled by one version of the compiler cannot be used by another version of the compiler.
Scala 3.0 is overcoming this limitation by introducing a new principled approach of metaprogramming. While this is an uncontested improvement, it also means that previous macro implementations have to be rewritten from the ground up.
Before rewriting a macro
Before getting deep into reimplementing a macro your should check if it can be supported using Scala 3.0 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
inline
and the metaprogramming features inscala.compiletime
to reimplement my logic? - Can I use the simpler and safer expression based macros to implement my macro?
- Do I really need to have access to the raw AST trees?
You can find references to these concepts in the macro tutorial.
Macro turorial
Additional Resources
Documentation:
- Scala 3.0 Documentation
- Macros: The Plan For Scala 3
- Examples - a repository with small, self-contained examples of various tasks done with Scala 3 macros.
Talks:
Projects: