0Pricing
Scala for Backend Engineering & Functional Programming · Lektion

Quotes und Splices

Code als Daten

Quotes und Splices ist eine kostenlose Scala for Backend Engineering & Functional Programming-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Scala for Backend Engineering & Functional Programming-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Scala for Backend Engineering & Functional Programming-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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 into Expr[T]
  • ${ } splices an Expr into code
  • quote pattern matching to destructure code
  • Type[T] and the reflect API

Next: practical macros.

Häufig gestellte Fragen

Ist die Lektion „Quotes und Splices“ kostenlos?

Ja — der vollständige Text von „Quotes und Splices“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Scala for Backend Engineering & Functional Programming-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Scala for Backend Engineering & Functional Programming-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Quotes und Splices“?

Code als Daten Du übst Scala for Backend Engineering & Functional Programming mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Scala for Backend Engineering & Functional Programming zu starten?

Keine Vorkenntnisse erforderlich. Scala for Backend Engineering & Functional Programming auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.

Wie lange dauert die Lektion „Quotes und Splices“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Scala for Backend Engineering & Functional Programming-Lektion Code schreiben und ausführen?

Ja. Jede Scala for Backend Engineering & Functional Programming-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Inline-Methoden
  2. Makro-Grundlagen
  3. Quotes und Splices
  4. Praktische Makros
← Zurück zu Scala for Backend Engineering & Functional Programming