Skip to content
العربية

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.

  • 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.
One function for every type
# دالة واحدة لكل نوع: ن معامل نوع يُستنتج من الوسيط
داله اول_او[ن](عناصر قائمة[ن], بديل ن) ن:
اذا طول(عناصر) == 0:
ارجع بديل
ارجع عناصر[0]
اسماء قائمة[نص] = ["سلمى", "خالد"]
فارغة قائمة[عدد64] = []
اطبع(اول_او(اسماء, "لا أحد"))
اطبع(اول_او(فارغة, -1))
اطبع(اول_او([2.5, 4.0], 0.0))
حرر(اسماء)
حرر(فارغة)
Output recorded from the compilermeem معمم_اول.م · meem 0.17.3+434e13f
سلمى-12.5
  • 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](...).
A pair of any two types
# هيكل معمّم: زوج يحمل قيمتين من أي نوعين
هيكل زوج[ا, ب]:
اول ا
ثان ب
ع = زوج("العمر", 31)
ط = زوج("الطول", 1.75)
اطبع(ع.اول, ": ", ع.ثان)
اطبع(ط.اول, ": ", ط.ثان)
ص = زوج[نص, كسر64]("الوزن", 70.0)
اطبع(ص)
Output recorded from the compilermeem معمم_زوج.م · meem 0.17.3+434e13f
العمر: 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.
The constraint and a derived trait
# سمة وقيد: الدالة تقبل كل نوع يحقق «قابل_للمقارنة»
هيكل نسخة يحقق قابل_للمقارنة:
رئيسي عدد64
فرعي عدد64
داله الاكبر[ن يحقق قابل_للمقارنة](عناصر قائمة[ن]) ن:
ك = عناصر[0]
لكل ع في عناصر:
اذا ع > ك:
ك = ع
ارجع ك
اطبع(الاكبر([3, 11, 7]))
اطبع(الاكبر(["تفاح", "موز", "برتقال"]))
نسخ_ قائمة[نسخة] = [نسخة(1, 2), نسخة(0, 17), نسخة(1, 0)]
اطبع(الاكبر(نسخ_))
حرر(نسخ_)
Output recorded from the compilermeem معمم_سمة.م · meem 0.17.3+434e13f
11موزنسخة { رئيسي: 1, فرعي: 2 }

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
Solution
# كم مرة تظهر قيمة في قائمة، لأي نوع يُقارن بالمساواة
داله عد_مرات[ن يحقق قابل_للمساواة](عناصر قائمة[ن], قيمة ن) عدد64:
م = 0
لكل ع في عناصر:
اذا ع == قيمة:
م += 1
ارجع م
اطبع(عد_مرات([1, 3, 1, 1, 2], 1))
اطبع(عد_مرات(["نعم", "لا", "نعم"], "نعم"))
اطبع(عد_مرات([صواب, خطاء, خطاء], خطاء))
Output recorded from the compilermeem معمم_حل.م · meem 0.17.3+434e13f
322

Passing a type that does not implement the trait the constraint asks for (E0521):

Causes the error
هيكل نقطة:
س عدد64
ص عدد64
داله الاكبر[ن يحقق قابل_للمقارنة](عناصر قائمة[ن]) ن:
ارجع عناصر[0]
اطبع(الاكبر([نقطة(1, 2)]))
Output recorded from the compilermeem معمم_خطأ.م · meem 0.17.3+434e13f
​[خطأ دلالي E0521] سطر 8:6 - 'نقطة' لا يحقق 'قابل_للمقارنة' التي تطلبها 'الاكبر'   8 | اطبع(الاكبر([نقطة(1, 2)]))     |      ^~~~~~  تلميح: أعلن التحقيق في تعريف النوع:  هيكل نقطة يحقق قابل_للمقارنة:  ثم عرّف دوال السمة، أو اتركها تُشتق بترتيب الحقول.

Exit code: 2

In the error catalog: E0521

The fix, as the hint says: هيكل نقطة يحقق قابل_للمقارنة:.

Generics in the reference · Migrating from 0.16 to 0.17 · The collections library