Generic Functions over Traits
Write one function for many types.
Generic Functions over Traits is a free Mojo Academy lesson on CoddyKit — lesson 4 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.
One Function, Many Types
The real power of traits is generic code: write a function once and it works for every conforming type. 🚀
A Trait as a Bound
You use a trait as a bound on a generic parameter, promising the type has the methods you call.
Generic Parameter Syntax
Inside square brackets you name a type parameter and the trait it must satisfy, then use it like any type.
fn announce[T: Greetable](item: T):
print(item.greet())Calling Trait Methods
Within the function you may call any method the trait guarantees. The compiler knows greet exists on T.
Works for Every Conformer
Pass a Dog or a Robot to the same function. Both conform to Greetable, so both are accepted seamlessly.
announce(Dog("Rex"))
announce(Robot())Type Safety Stays
Generics are not loose. If a type does not satisfy the bound, the compiler refuses it before the program runs.
No Runtime Penalty
Mojo specializes generic code at compile time, generating a fast version per type. Flexibility costs no speed.
Less Duplication
Without generics you would copy a function for each type. A trait bound lets one definition serve them all, cutting duplication.
Multiple Bounds
A parameter can require several traits at once, ensuring the type has every capability the function needs.
Designing Around Traits
Good Mojo APIs ask for the smallest trait they need. Functions stay flexible while expressing exact requirements.
The Whole Picture
Trait, conformance, and generics combine: define a contract, implement it, then write one reusable function over it.
fn print_all[T: Stringable](x: T):
print(String(x))Quick Check
Why use a trait bound on a generic function?
Recap
Generic functions with a trait bound work across all conforming types, staying type-safe and fast. You now write reusable Mojo! 🎉
Frequently asked questions
Is the “Generic Functions over Traits” lesson free?
Yes — the full text of “Generic Functions over Traits” 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 “Generic Functions over Traits”?
Write one function for many types. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Generic Functions over Traits” 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
- What Is a Trait?
- Conforming a Struct to a Trait
- Built-in Traits Like Copyable
- Generic Functions over Traits