البنى ذات المعاملات
ابنِ حاويات عامة وآمنة الأنواع
البنى ذات المعاملات درس مجاني في Mojo Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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. 🎯
الأسئلة الشائعة
هل درس «البنى ذات المعاملات» مجاني؟
نعم — نص درس «البنى ذات المعاملات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Mojo Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Mojo Academy 4 دروس في المجموع.
ماذا ستتعلم في «البنى ذات المعاملات»؟
ابنِ حاويات عامة وآمنة الأنواع تتمرن على Mojo Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Mojo Academy؟
لا تُشترط خبرة سابقة. Mojo Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «البنى ذات المعاملات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Mojo Academy هذا؟
نعم. كل درس في Mojo Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- المعاملات مقابل الوسيطات
- الدوال ذات المعاملات
- البنى ذات المعاملات
- لماذا تمنح الترجمة سرعة أكبر