Macros in Scala 3

Tutorial

Language
This doc page is specific to Scala 3, and may cover new concepts not available in Scala 2. Unless otherwise stated, all the code examples in this page assume you are using Scala 3.

This tutorial covers all the features involved in writing macros in Scala 3.

The metaprogramming API of Scala 3 is designed in layers to gradually support different levels of use-cases. Each successive layer exposes additional abstractions and offers more fine-grained control.

  • As a starting point, the new inline feature allows some abstractions (values and methods) to be marked as statically reducible. It provides the entry point for macros and other metaprogramming utilities.

  • Compile-time operations offer additional metaprogramming utilities that can be used within inline methods (for example to improve error reporting), without having to define a macro.

  • Starting from inline methods, macros are programs that explicitly operate on programs.

    • Macros can be defined in terms of a high-level API of quoted expressions, that admits simple construction and deconstruction of programs expressions.

    • Macros can also be defined in terms of a more low-level API of Reflection, that allows detailed inspection of programs.

The tutorial uses the API of Scala 3.0.0-RC3. The API had many small changes in this revision.

🚧 We are still in the process of writing the tutorial. You can help us 🚧

Contributors to this page: