인용과 스플라이스
코드를 데이터로 다룹니다
인용과 스플라이스은(는) CoddyKit의 무료 Scala for Backend Engineering & Functional Programming 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Scala for Backend Engineering & Functional Programming 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Scala for Backend Engineering & Functional Programming 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
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.
자주 묻는 질문
“인용과 스플라이스” 강의는 무료인가요?
네 — “인용과 스플라이스” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Scala for Backend Engineering & Functional Programming 강의 전체를 잠금 해제할 수 있습니다. Scala for Backend Engineering & Functional Programming 강의에는 총 4개의 강의가 포함되어 있습니다.
“인용과 스플라이스”에서 뭘 배우나요?
코드를 데이터로 다룹니다 브라우저에서 직접 실행하는 실습 코드로 Scala for Backend Engineering & Functional Programming을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Scala for Backend Engineering & Functional Programming을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Scala for Backend Engineering & Functional Programming은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.
“인용과 스플라이스” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Scala for Backend Engineering & Functional Programming 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Scala for Backend Engineering & Functional Programming 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.