Die __copyinit__-Methode
Definieren Sie, wie Ihr Typ sich selbst kopiert.
Die __copyinit__-Methode ist eine kostenlose Mojo Academy-Lektion auf CoddyKit. Dies ist Lektion 2 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.
Defining How to Copy
When a value is copied, Mojo calls a special method named __copyinit__. It builds a fresh instance from an existing one. 📋
It Runs on Assignment
Every time a copy happens, like plain assignment, __copyinit__ fires. It is the hook that defines what copying your type means.
The Method Signature
It takes the new instance to fill and a read-only existing value to copy from. You set each field on the new one.
fn __copyinit__(out self, existing: Self):
self.x = existing.xInitialize Every Field
Inside __copyinit__ your job is to give the new instance valid values for all of its fields, usually mirrored from existing.
Deep vs Shallow Copies
For heap data you decide: copy the pointer, or allocate and clone the buffer. A true deep copy gives full independence.
Why Independence Matters
A correct __copyinit__ ensures the copy owns its own data. Then editing the copy never reaches back and changes the original.
Often Derived for You
You rarely write it by hand. Marking a type Copyable lets Mojo synthesize a field-by-field __copyinit__ automatically.
@fieldwise_init
struct Point(Copyable):
var x: IntWhen to Write It Yourself
Hand-write __copyinit__ only when the default is wrong, like when you manage a raw buffer that needs cloning.
It Defines Copy Semantics
This method is literally what makes your type copyable. Without it, Mojo cannot duplicate your value on assignment.
Keep It Correct and Cheap
A good __copyinit__ is correct first and lean second. Copy only what you must so duplication stays affordable.
A Building Block of Safety
Together with moves, __copyinit__ gives Mojo its predictable value semantics. You control exactly how data is duplicated.
Quick Check
Lets check the copy initializer.
Recap
__copyinit__ defines how your type copies itself, building an independent instance. Mojo often derives it, but you can customize it. 🎯
Häufig gestellte Fragen
Ist die Lektion „Die __copyinit__-Methode“ kostenlos?
Ja — der vollständige Text von „Die __copyinit__-Methode“ 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 „Die __copyinit__-Methode“?
Definieren Sie, wie Ihr Typ sich selbst kopiert. 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 2 von 4.
Wie lange dauert die Lektion „Die __copyinit__-Methode“?
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
- Kopier- und Verschiebesemantik
- Die __copyinit__-Methode
- Die __moveinit__-Methode
- Destruktion und __del__