Struktury parametryzowane
Buduj generyczne kontenery bezpieczne typowo.
Struktury parametryzowane to bezpłatna lekcja Mojo Academy na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Mojo Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Mojo Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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. 🎯
Często zadawane pytania
Czy lekcja „Struktury parametryzowane” jest bezpłatna?
Tak — pełny tekst „Struktury parametryzowane” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Mojo Academy, przejdź na CoddyKit PRO. Kurs Mojo Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Struktury parametryzowane”?
Buduj generyczne kontenery bezpieczne typowo. Ćwiczysz Mojo Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Mojo Academy?
Nie wymagamy żadnego doświadczenia. Mojo Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.
Ile czasu zajmuje lekcja „Struktury parametryzowane”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Mojo Academy?
Tak. Każda lekcja Mojo Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Parametry a argumenty
- Funkcje parametryzowane
- Struktury parametryzowane
- Dlaczego kompilacja przyspiesza działanie