パラメーター化されたStruct
汎用的で型安全なコンテナを構築します
「パラメーター化されたStruct」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMojo Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Mojo Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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. 🎯
よくある質問
「パラメーター化されたStruct」レッスンは無料ですか?
はい。「パラメーター化されたStruct」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。
「パラメーター化されたStruct」で何を学びますか?
汎用的で型安全なコンテナを構築します ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Mojo Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「パラメーター化されたStruct」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMojo Academyレッスンでコードを書いて実行できますか?
はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- パラメーターと引数
- パラメーター化された関数
- パラメーター化されたStruct
- コンパイル時処理が高速化につながる理由