18. Generics and traits
To write a function or a struct once so that it works with many types, and to require what you need from the type with a trait.
Explanation
Section titled “Explanation”- A type parameter is a name in square brackets after the function’s name:
داله اول_او[ن](عناصر قائمة[ن], بديل ن) ن:(first or a fallback).نstands for some type (since 0.15.0). - You do not write the type at the call: the compiler infers it from the arguments, and copies the function for each type it is called with, so there is no run-time cost.
# دالة واحدة لكل نوع: ن معامل نوع يُستنتج من الوسيطداله اول_او[ن](عناصر قائمة[ن], بديل ن) ن: اذا طول(عناصر) == 0: ارجع بديل ارجع عناصر[0]
اسماء قائمة[نص] = ["سلمى", "خالد"]فارغة قائمة[عدد64] = []اطبع(اول_او(اسماء, "لا أحد"))اطبع(اول_او(فارغة, -1))اطبع(اول_او([2.5, 4.0], 0.0))حرر(اسماء)حرر(فارغة)- A generic struct (since 0.16.0):
هيكل زوج[ا, ب]:(pair) with fields of typesاandب. Construction infers them from the values, or you write them explicitly:زوج[نص, كسر64](...).
# هيكل معمّم: زوج يحمل قيمتين من أي نوعينهيكل زوج[ا, ب]: اول ا ثان ب
ع = زوج("العمر", 31)ط = زوج("الطول", 1.75)اطبع(ع.اول, ": ", ع.ثان)اطبع(ط.اول, ": ", ط.ثان)ص = زوج[نص, كسر64]("الوزن", 70.0)اطبع(ص)- A trait (since 0.17.0) is an ability a type implements, such as
قابل_للمساواة(equatable,==),قابل_للمقارنة(comparable,<and>) andقابل_للتجزئة(hashable, a dictionary key). Numbers and strings implement them. - The constraint
[ن يحقق قابل_للمقارنة]says that the function accepts only types that implement the trait. - Your struct implements it by declaring it in its definition:
هيكل نسخة يحقق قابل_للمقارنة:. If you do not write its functions, the compiler derives them field by field, in field order.
# سمة وقيد: الدالة تقبل كل نوع يحقق «قابل_للمقارنة»هيكل نسخة يحقق قابل_للمقارنة: رئيسي عدد64 فرعي عدد64
داله الاكبر[ن يحقق قابل_للمقارنة](عناصر قائمة[ن]) ن: ك = عناصر[0] لكل ع في عناصر: اذا ع > ك: ك = ع ارجع ك
اطبع(الاكبر([3, 11, 7]))اطبع(الاكبر(["تفاح", "موز", "برتقال"]))نسخ_ قائمة[نسخة] = [نسخة(1, 2), نسخة(0, 17), نسخة(1, 0)]اطبع(الاكبر(نسخ_))حرر(نسخ_)Exercise
Section titled “Exercise”Write a generic function عد_مرات (count occurrences) that counts how many times a value appears in a list, for any type that implements قابل_للمساواة, and try it on numbers, strings and booleans.
Show the solution
# كم مرة تظهر قيمة في قائمة، لأي نوع يُقارن بالمساواةداله عد_مرات[ن يحقق قابل_للمساواة](عناصر قائمة[ن], قيمة ن) عدد64: م = 0 لكل ع في عناصر: اذا ع == قيمة: م += 1 ارجع م
اطبع(عد_مرات([1, 3, 1, 1, 2], 1))اطبع(عد_مرات(["نعم", "لا", "نعم"], "نعم"))اطبع(عد_مرات([صواب, خطاء, خطاء], خطاء))Common mistakes
Section titled “Common mistakes”Passing a type that does not implement the trait the constraint asks for (E0521):
هيكل نقطة: س عدد64 ص عدد64
داله الاكبر[ن يحقق قابل_للمقارنة](عناصر قائمة[ن]) ن: ارجع عناصر[0]
اطبع(الاكبر([نقطة(1, 2)]))Exit code: 2
In the error catalog: E0521
The fix, as the hint says: هيكل نقطة يحقق قابل_للمقارنة:.
See also
Section titled “See also”Generics in the reference · Migrating from 0.16 to 0.17 · The collections library