0Pricing
Scala for Backend Engineering & Functional Programming · درس

أساسيات Macros

شيفرة وقت الترجمة

أساسيات Macros درس مجاني في Scala for Backend Engineering & Functional Programming على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Scala for Backend Engineering & Functional Programming، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Scala for Backend Engineering & Functional Programming 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

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.

الأسئلة الشائعة

هل درس «أساسيات Macros» مجاني؟

نعم — نص درس «أساسيات Macros» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Scala for Backend Engineering & Functional Programming، انتقل إلى CoddyKit PRO. تتضمن دورة Scala for Backend Engineering & Functional Programming 4 دروس في المجموع.

ماذا ستتعلم في «أساسيات Macros»؟

شيفرة وقت الترجمة تتمرن على Scala for Backend Engineering & Functional Programming مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Scala for Backend Engineering & Functional Programming؟

لا تُشترط خبرة سابقة. Scala for Backend Engineering & Functional Programming على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «أساسيات Macros»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Scala for Backend Engineering & Functional Programming هذا؟

نعم. كل درس في Scala for Backend Engineering & Functional Programming يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. الدوال Inline
  2. أساسيات Macros
  3. الاقتباسات والدمج
  4. Macros عملية
← العودة إلى Scala for Backend Engineering & Functional Programming