The __copyinit__ Method
Define how your type copies itself.
The __copyinit__ Method is a free Mojo Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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. 🎯
Frequently asked questions
Is the “The __copyinit__ Method” lesson free?
Yes — the full text of “The __copyinit__ Method” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.
What will I learn in “The __copyinit__ Method”?
Define how your type copies itself. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Mojo Academy?
No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The __copyinit__ Method” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Mojo Academy lesson?
Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Copy vs Move Semantics
- The __copyinit__ Method
- The __moveinit__ Method
- Destruction and __del__