The Scala 2 macros are compiler dependent by design. Therefore it is impossible to compile or even consume them in Scala 3.
In this document, we provide guidelines to help you migrate your macro library to Scala 3.
Before rewriting a macro
Before getting deep into reimplementing a macro your 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?
- I really need to have access to the raw AST trees
A good reference for this is Shapeless 3. It uses Scala 3 features most of the time for a feature that would have been macros in Scala 2 and only uses macros where absolutely necessary.
Defining a project that cross compiles macros
If you are already cross-compiling your macro for different versions of Scala 2 and you have sources folders for each version, then you can add an extra source folder for Scala 3.
- SBT examples:
- TODO find a simple example project
- Mill examples
- Scala 3 Documentation
- Macros: The Plan For Scala 3
- Examples - a repository with small, self-contained examples of various tasks done with Scala 3 macros.