Intro to Scala 3 macros

Click for: original source

What is macro? A common explanation given is that a macro is a program that is able to take code as an input and output code. While it’s true, it might not immediately make sense since Scala programmers are often familiar with higher-order functions like (map {…}) and by-name parameter, which on the surface it might seem like it is passing a block of code around. By @eed3si9n.

The article is split into these sections:

  • Quotes and Splices
  • Quotes Reflection API
  • Tree
  • Printer
  • Literal
  • FromExpr typeclass
  • Position
  • TypeRepr
  • AppliedType

… and more. You will also get the code examples for each concept explained.

Macros in Scala 3 brings out a different level of capability in programming, which is to manipulate the shape of source code using Scala syntax itself, and also to directly interact with the type system. Where possible, we should opt to use the Scala syntax to construct the quoted code instead of programmatically constructing the AST via (Quote) Reflection API. Good read!

[Read More]

Tags scala java jvm programming