Parameterized Structs
Build generic, type-safe containers.
Parameterized Structs is a free Mojo Academy lesson on CoddyKit — lesson 3 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.
Structs Take Parameters Too
A parameterized struct takes compile-time values in brackets, letting one definition become many type-safe, specialized containers. 📦
struct Box[T: AnyType]:
var value: TA Type Parameter
The T here is a type parameter. It stands in for whatever type you fill in when you actually create the struct.
Naming the Concrete Type
When you use the struct, you supply the type in brackets, turning the generic blueprint into a concrete, fixed type.
var b = Box[Int](42)Many Types, One Blueprint
The same blueprint gives you Box[Int], Box[Float64], and more, each a distinct type the compiler tailors for you.
var f = Box[Float64](3.14)Numeric Parameters
Parameters need not be types. A numeric parameter can fix a size, like the length of a fixed-capacity array.
struct Buffer[size: Int]:
var count: IntSize Known Up Front
With size as a parameter, the compiler knows the exact layout in advance and can lay out memory with zero runtime guesswork.
Methods See the Parameters
Inside the struct, every method can use the parameters freely, so behavior adapts to the type or size you chose.
struct Pair[T: AnyType]:
var a: T
var b: TType Safety for Free
A Box[Int] simply will not accept a Float. The compiler enforces the parameter, catching mix-ups before the program ever runs.
Multiple Parameters
A struct can list several parameters at once, combining types and sizes to describe exactly the container you need.
struct Grid[T: AnyType, rows: Int, cols: Int]:
var total: IntHow SIMD Uses This
The built-in SIMD type is itself a parameterized struct: its element type and width are both compile-time parameters.
var v = SIMD[DType.float32, 4](1, 2, 3, 4)Generic and Fast
Parameterized structs give you Python-like generics with none of the overhead, since each version is specialized at compile time.
Quick Check
Recall what a parameterized struct can hold in its brackets.
Recap
You built parameterized structs: brackets hold types or sizes, one blueprint yields many specialized types, all checked and optimized at compile time. 🎯
Frequently asked questions
Is the “Parameterized Structs” lesson free?
Yes — the full text of “Parameterized Structs” 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 “Parameterized Structs”?
Build generic, type-safe containers. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Parameterized Structs” 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
- Parameters vs Arguments
- Parameterized Functions
- Parameterized Structs
- Why Compile-Time Wins Speed