0Pricing
Scala for Backend Engineering & Functional Programming · Lektion

Inline-Methoden

Scala-3-inline

Inline-Methoden ist eine kostenlose Scala for Backend Engineering & Functional Programming-Lektion auf CoddyKit. Dies ist Lektion 1 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.

What Is inline?

The inline keyword in Scala 3 tells the compiler to expand a definition at the call site during compilation. It is the entry point to metaprogramming, before full macros.

An Inline Method

Mark a method inline and its body is substituted wherever it is called. This can remove call overhead and enable compile-time specialization.

inline def square(x: Int): Int = x * x

val r = square(5) // compiles to 5 * 5

Inline Parameters

An inline parameter forces its argument to be inlined too. If the argument is a constant, the compiler can fold it into a literal.

inline def power(x: Int, inline n: Int): Int =
  if (n == 0) 1 else x * power(x, n - 1)

Constant Folding

When inline parameters are literals, branches and recursion can be fully resolved at compile time, leaving only the final expression in the bytecode.

inline def power(x: Int, inline n: Int): Int =
  if (n == 0) 1 else x * power(x, n - 1)

val c = power(2, 3) // expands to 2 * 2 * 2 * 1

inline if and inline match

Inside an inline method, inline if and inline match are resolved at compile time when the scrutinee is statically known, eliminating dead branches.

inline def label(inline b: Boolean): String =
  inline if (b) "yes" else "no"

Transparent Inline

transparent inline lets the method's return type be refined per call site — the compiler may infer a more specific type than the declared one.

transparent inline def first(inline b: Boolean): Any =
  inline if (b) 1 else "x" // type can be Int or String

compiletime.error

The scala.compiletime package offers tools like error to fail compilation with a custom message — useful for validating inline arguments.

import scala.compiletime.error

inline def positive(inline n: Int): Int =
  inline if (n <= 0) error("n must be positive") else n

Inline Values

An inline val is a true compile-time constant. It can be used where literals are required, such as in other inline computations.

inline val MaxRetries = 3

inline def retries: Int = MaxRetries

Performance vs Code Size

Inlining trades code size for speed and compile-time guarantees. Overusing it on large bodies bloats bytecode, so reserve it for small, hot, or specialization-critical methods.

Runnable Without Inline

Here is the same squaring logic as an ordinary self-contained program. The inline version would produce identical results with the multiplication expanded at the call site.

object Main {
  def square(x: Int): Int = x * x
  def main(args: Array[String]): Unit = {
    println(square(5)) // 25
  }
}

Inline Is the Foundation

Macros build on inline methods: an inline def can call a macro to generate code. Mastering inline first makes the macro layer much easier.

Quick Check

What does adding transparent to an inline method change?

Recap

You learned Scala 3 inlining:

  • inline def expands at the call site
  • inline parameters and constant folding
  • inline if / inline match
  • transparent inline, inline val, compiletime.error

Next: macro basics.

Häufig gestellte Fragen

Ist die Lektion „Inline-Methoden“ kostenlos?

Ja — der vollständige Text von „Inline-Methoden“ 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 „Inline-Methoden“?

Scala-3-inline 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 1 von 4.

Wie lange dauert die Lektion „Inline-Methoden“?

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