المستقبِلات المؤشرية
المستقبِلات المؤشرية
يمكنك التصريح بتوابع ذات مستقبِلات مؤشرية.
يعني هذا أن نوع المستقبِل يُكتب بالصيغة الحرفية *T لنوع ما T.
(كذلك، لا يمكن أن يكون T نفسه مؤشرًا مثل *int.)
على سبيل المثال، التابع Scale هنا معرّف على *Vertex.
يمكن للتوابع ذات المستقبِلات المؤشرية تعديل القيمة التي يشير إليها المستقبِل
(كما يفعل Scale هنا).
ولأن التوابع غالبًا ما تحتاج إلى تعديل مستقبِلاتها، فإن المستقبِلات المؤشرية أكثر
شيوعًا من المستقبِلات القيمية.
جرّب إزالة * من التصريح بالدالة Scale في السطر 16
ولاحظ كيف يتغيّر سلوك البرنامج.
عند استخدام مستقبِل قيمي، يعمل التابع Scale على نسخة من القيمة الأصلية
من النوع Vertex.
(وهذا هو السلوك نفسه لأي وسيط آخر لدالة.)
يجب أن يكون للتابع Scale مستقبِل مؤشري لتغيير قيمة Vertex
المصرّح بها في الدالة main.
النص الأصلي بالإنجليزية
Pointer receivers
You can declare methods with pointer receivers.
This means the receiver type has the literal syntax *T for some type T.
(Also, T cannot itself be a pointer such as *int.)
For example, the Scale method here is defined on *Vertex.
Methods with pointer receivers can modify the value to which the receiver
points (as Scale does here).
Since methods often need to modify their receiver, pointer receivers are more
common than value receivers.
Try removing the * from the declaration of the Scale function on line 16
and observe how the program's behavior changes.
With a value receiver, the Scale method operates on a copy of the original
Vertex value.
(This is the same behavior as for any other function argument.)
The Scale method must have a pointer receiver to change the Vertex value
declared in the main function.
هذا الدرس للقراءة فقط؛ جرّب المثال في الدرس التالي.