Quotes and Splices
Code as data.
Quotes and Splices is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 3 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.
Code as Data
Scala 3 metaprogramming treats code as data. A quote captures a piece of code as an Expr, and a splice inserts an Expr back into code.
The Quote: '{ ... }
'{ expr } turns an expression into an Expr[T]. The code is not run — it becomes a value the macro can manipulate.
import scala.quoted.*
def greeting(using Quotes): Expr[String] = '{ "Hello, " + "world" }The Splice: ${ ... }
${ expr } inserts an Expr into a surrounding quote, stitching generated fragments together. Quote and splice are inverses.
import scala.quoted.*
def doubled(x: Expr[Int])(using Quotes): Expr[Int] = '{ ${ x } * 2 }Splicing Lifted Values
Inside a quote you can splice a value lifted with Expr(...) to embed a compile-time constant into generated code.
import scala.quoted.*
def addN(x: Expr[Int], n: Int)(using Quotes): Expr[Int] =
'{ ${ x } + ${ Expr(n) } }Building Code Recursively
Macros build complex expressions by combining quotes and splices in loops or recursion — for example unrolling a power into repeated multiplication.
import scala.quoted.*
def pow(x: Expr[Double], n: Int)(using Quotes): Expr[Double] =
if (n == 0) '{ 1.0 } else '{ ${ x } * ${ pow(x, n - 1) } }Pattern Matching on Quotes
You can destructure code by matching against quote patterns. This inspects the shape of an expression to optimize or rewrite it.
import scala.quoted.*
def optimize(e: Expr[Int])(using Quotes): Expr[Int] = e match {
case '{ ($a: Int) + 0 } => a
case _ => e
}Type[T] and Quoted Types
Just as Expr[T] carries an expression, Type[T] carries a type. Use '[T] to quote a type and given Type[T] to splice it where a type is needed.
import scala.quoted.*
def typeName[T](using t: Type[T], q: Quotes): Expr[String] =
Expr(Type.show[T])The reflect API
For lower-level work, quotes.reflect exposes the abstract syntax tree (Term, Symbol, TypeRepr) so you can inspect or build code beyond what quotes express directly.
import scala.quoted.*
def show(e: Expr[Any])(using q: Quotes): Expr[String] = {
import q.reflect.*
Expr(e.asTerm.show)
}Round-Trip Between Levels
Convert an Expr to a Term with asTerm and back with asExpr / asExprOf[T]. This bridges the high-level quote API and the reflection API.
import scala.quoted.*
def ident[T: Type](e: Expr[T])(using q: Quotes): Expr[T] = {
import q.reflect.*
e.asTerm.asExprOf[T]
}Runtime Analog
The recursive power macro above expands to plain multiplications. This self-contained runtime version shows the same result the generated code would produce.
object Main {
def pow(x: Double, n: Int): Double =
if (n == 0) 1.0 else x * pow(x, n - 1)
def main(args: Array[String]): Unit = {
println(pow(3.0, 2)) // 9.0
}
}Quote and Splice Are Inverses
Remember the duality: '{ } lifts code into an Expr; ${ } drops an Expr back into code. Nesting them correctly is the heart of building macros.
Quick Check
What does the splice operator ${ x } do inside a quote?
Recap
You learned quotes and splices:
'{ }quotes code intoExpr[T]${ }splices anExprinto code- quote pattern matching to destructure code
Type[T]and thereflectAPI
Next: practical macros.
Frequently asked questions
Is the “Quotes and Splices” lesson free?
Yes — the full text of “Quotes and Splices” 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 “Quotes and Splices”?
Code as data. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Quotes and Splices” 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