Hilfreiche Typannotationen
Fügen Sie Typen hinzu, damit Mojo optimieren und Fehler erkennen kann.
Hilfreiche Typannotationen ist eine kostenlose Mojo Academy-Lektion auf CoddyKit. Dies ist Lektion 4 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.
What Is a Type Annotation?
A type annotation states a value's type right in the declaration, so both you and Mojo know exactly what it holds.
Annotating a var
Add a colon and the type after the name. Here : Int promises that count will only ever hold whole numbers.
var count: Int = 0Inference vs Annotation
Without an annotation Mojo infers the type from the value. Writing it yourself makes your intent explicit and clear.
var x = 5
var y: Int = 5Catching Mistakes Early
An annotation lets Mojo reject a wrong value at compile time. Assigning text to an Int becomes an immediate error.
var count: Int = "oops" # errorAnnotations Aid Speed
Knowing the exact type ahead of time, Mojo can generate faster machine code instead of checking types as it runs.
Annotating Floats and Bools
Annotations work for every type. Spell out Float64 or Bool to document precisely what each value is.
var ratio: Float64 = 0.5
var ok: Bool = TrueAnnotating Strings
Mark text values with String so readers instantly see that a var carries words rather than numbers.
var name: String = "Ada"Annotations as Documentation
A type next to a name tells the next reader, including future you, what to expect without hunting for the value.
Required in fn Functions
Strict fn functions require type annotations on their parameters, which is where this habit pays off most.
fn add(a: Int, b: Int):
print(a + b)Annotate at Boundaries
Even when inference works, annotate the values others rely on. Clear types at boundaries prevent confusion across your code.
Types Are a Safety Net
Annotations turn silent bugs into loud errors and unlock speed. A little typing now saves real debugging later.
Quick Check
You annotate a var as Int but try to assign it the text "oops". What happens in Mojo?
Recap
A type annotation names a value's type with a colon, catching errors early and helping Mojo run faster. 🎯
Häufig gestellte Fragen
Ist die Lektion „Hilfreiche Typannotationen“ kostenlos?
Ja — der vollständige Text von „Hilfreiche Typannotationen“ 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 „Hilfreiche Typannotationen“?
Fügen Sie Typen hinzu, damit Mojo optimieren und Fehler erkennen kann. 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 4 von 4.
Wie lange dauert die Lektion „Hilfreiche Typannotationen“?
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
- Werte mit var deklarieren
- Int, Float und Bool
- Strings und Text
- Hilfreiche Typannotationen