0Pricing
Scala for Backend Engineering & Functional Programming · Ders

Makro Temelleri

Derleme zamanı kodu

Makro Temelleri, CoddyKit'te ücretsiz bir Scala for Backend Engineering & Functional Programming dersidir. Bu, 4 dersinin 2. 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.

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
  • Quotes context and Expr[T]
  • lifting with Expr(...) and extracting with .value
  • error reporting and hygiene

Next: quotes and splices.

Sıkça Sorulan Sorular

“Makro Temelleri” dersi ücretsiz mi?

Evet — “Makro Temelleri” 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.

“Makro Temelleri” dersinde ne öğreneceğim?

Derleme zamanı kodu 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 2. dersidir.

“Makro Temelleri” 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

  1. Satır İçi Yöntemler
  2. Makro Temelleri
  3. Alıntılar ve Eklemeler
  4. Pratik Makrolar
← Scala for Backend Engineering & Functional Programming Sayfasına Dön