Bedingte Kompilierung
Verzweigen Sie Codepfade zur Compile-Zeit.
Bedingte Kompilierung ist eine kostenlose Mojo Academy-Lektion auf CoddyKit. Dies ist Lektion 3 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 Mojo Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Choosing Code at Compile Time
Conditional compilation picks which code path to build based on compile-time values, so the unused branch never reaches the final program. ⚙️
The @parameter if Statement
Add @parameter to an if whose condition is known at compile time, and Mojo keeps only the branch that matches.
@parameter
if width == 1:
scalar_path()
else:
vector_path()The Condition Must Be Static
The test has to use parameters or aliases, values the compiler already knows, not data that only appears while the program runs.
alias DEBUG = False
@parameter
if DEBUG:
print("tracing")Dead Branches Disappear
The branch that fails the condition is simply not compiled, so it adds no size and costs nothing at runtime.
Specialize by Type
Use it to switch logic based on a type parameter, choosing the best implementation for floats versus integers, for example.
@parameter
if T.is_floating_point():
use_fma()
else:
use_int_mul()Branch on Hardware Facts
Pair conditional compilation with platform checks to emit the right kernel for the CPU or accelerator you are targeting.
Feature Flags Without Cost
Gate optional features behind an alias flag; when it is False the whole feature compiles away, leaving a lean binary.
Different From a Runtime if
A normal if always keeps both branches in the binary and tests at runtime; @parameter if decides once, at build time, and drops the rest.
Combine With Other Metaprogramming
Conditional compilation mixes well with parameterized functions and unrolled loops, letting each specialization take its own optimal path.
One Source, Many Targets
From a single definition you generate tailored builds for each platform or type, with no runtime branching tax to pay. 🚀
Keep Both Paths Valid
Each branch must still compile on its own when chosen, so write every path to be correct for the parameter values that select it.
Quick Check
Compare @parameter if with a normal if.
Recap
@parameter if chooses code paths at compile time: dead branches vanish, you specialize by type or platform, and the binary stays lean. 🎯
Häufig gestellte Fragen
Ist die Lektion „Bedingte Kompilierung“ kostenlos?
Ja — der vollständige Text von „Bedingte Kompilierung“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Mojo Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Bedingte Kompilierung“?
Verzweigen Sie Codepfade zur Compile-Zeit. Du übst Mojo Academy 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 Mojo Academy zu starten?
Keine Vorkenntnisse erforderlich. Mojo Academy 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 3 von 4.
Wie lange dauert die Lektion „Bedingte Kompilierung“?
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 Mojo Academy-Lektion Code schreiben und ausführen?
Ja. Jede Mojo Academy-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
- Parametrische Algorithmen
- Schleifen zur Compile-Zeit entrollen
- Bedingte Kompilierung
- Constraints und statische Prüfungen