Alıntılar ve Eklemeler
Veri olarak kod
Alıntılar ve Eklemeler, CoddyKit'te ücretsiz bir Scala for Backend Engineering & Functional Programming dersidir. Bu, 4 dersinin 3. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Scala for Backend Engineering & Functional Programming öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Scala for Backend Engineering & Functional Programming kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
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.
Sıkça Sorulan Sorular
“Alıntılar ve Eklemeler” dersi ücretsiz mi?
Evet — “Alıntılar ve Eklemeler” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Scala for Backend Engineering & Functional Programming kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Scala for Backend Engineering & Functional Programming kursu toplamda 4 dersten oluşur.
“Alıntılar ve Eklemeler” dersinde ne öğreneceğim?
Veri olarak kod Scala for Backend Engineering & Functional Programming ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Scala for Backend Engineering & Functional Programming öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Scala for Backend Engineering & Functional Programming, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 3. dersidir.
“Alıntılar ve Eklemeler” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Scala for Backend Engineering & Functional Programming dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Scala for Backend Engineering & Functional Programming dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Satır İçi Yöntemler
- Makro Temelleri
- Alıntılar ve Eklemeler
- Pratik Makrolar