Macros Basics
Compile-time code.
Macros Basics is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Macro?
A macro is code that runs at compile time to generate other code. Scala 3 macros let you inspect arguments, build expressions, and emit them into the program before it is compiled.
Inline + Macro Pattern
A macro is invoked from an inline def that delegates to a method marked with ${ ... }. The inline def is the public API; the macro impl runs in the compiler.
import scala.quoted.*
inline def power(x: Double, inline n: Int): Double =
${ powerImpl('x, 'n) }The Quotes Context
Every macro implementation takes an implicit Quotes context. It grants access to the reflection API and the ability to build and splice expressions.
import scala.quoted.*
def powerImpl(x: Expr[Double], n: Expr[Int])(using Quotes): Expr[Double] =
???Expr[T]
Expr[T] represents a typed expression of type T as compile-time data. Macros receive and return Expr values.
Lifting Values to Expr
Expr(value) lifts a runtime value computed in the macro into an expression to splice back. This works for any type with a ToExpr instance.
import scala.quoted.*
def constImpl(using Quotes): Expr[Int] = Expr(42)Extracting Constant Arguments
n.value (or n.valueOrAbort) extracts the constant behind an Expr when the argument is statically known, so the macro can branch on it.
import scala.quoted.*
def powerImpl(x: Expr[Double], n: Expr[Int])(using Quotes): Expr[Double] = {
val exp = n.valueOrAbort
??? // build x*x*...*x exp times
}Reporting Errors
Use quotes.reflect.report.errorAndAbort to emit a compile error with a message and location when the macro's preconditions are not met.
import scala.quoted.*
def check(n: Expr[Int])(using q: Quotes): Expr[Int] = {
import q.reflect.*
val v = n.valueOrAbort
if (v < 0) report.errorAndAbort("must be >= 0")
Expr(v)
}Where Macros Live
Macro implementations must be compiled before the code that uses them — typically in a separate file or module. The compiler executes them while compiling the caller.
Common Use Cases
Macros power:
- typeclass derivation (JSON codecs, etc.)
- compile-time validation (regex, SQL)
- logging that captures source positions
- zero-cost abstractions
Safety and Hygiene
Scala 3 macros are hygienic: generated identifiers cannot accidentally capture user variables, and the type system checks generated code, preventing many classic macro bugs.
Runtime Equivalent
A macro that computes a power at compile time produces the same result as this self-contained runtime version — but with the multiplications baked in by the compiler.
object Main {
def power(x: Double, n: Int): Double =
if (n == 0) 1.0 else x * power(x, n - 1)
def main(args: Array[String]): Unit = {
println(power(2.0, 3)) // 8.0
}
}Quick Check
What type represents a typed compile-time expression that macros receive and return?
Recap
You learned macro basics:
- macros run at compile time via the
inline def ... ${ }pattern Quotescontext andExpr[T]- lifting with
Expr(...)and extracting with.value - error reporting and hygiene
Next: quotes and splices.
Frequently asked questions
Is the “Macros Basics” lesson free?
Yes — the full text of “Macros Basics” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.
What will I learn in “Macros Basics”?
Compile-time code. You practise Scala for Backend Engineering & Functional Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Scala for Backend Engineering & Functional Programming?
No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Macros Basics” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Scala for Backend Engineering & Functional Programming lesson?
Yes. Every Scala for Backend Engineering & Functional Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Inline Methods
- Macros Basics
- Quotes and Splices
- Practical Macros