Delegates: Difference between revisions
Content added Content deleted
Line 1,360: | Line 1,360: | ||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
<lang M2000 Interpreter> |
<lang M2000 Interpreter> |
||
Module Version2 { |
|||
class Delegator { |
|||
\\ Second version |
|||
private: |
|||
\\ the delegate is a pointer to group |
|||
\\ 1. We pass parameters to function operations$(), $ means that this function return string value |
|||
group null |
|||
\\ 2. We see how this can be done with pointers to group |
|||
public: |
|||
global doc$ \\ first define a global (for this module) to log output |
|||
function operation$ { |
|||
document doc$="Output:"+{ |
|||
if not .delegate is .null then |
|||
=.delegate=>operation$() |
|||
else |
|||
= "Default implementation" |
|||
end if |
|||
} |
|||
class: |
|||
Module Delegator { |
|||
class none {} |
|||
.null->none() |
|||
If match("G") then .delegate->(group) else .delegate<=.null |
|||
} |
} |
||
class Delegator { |
|||
private: |
|||
group delegate |
|||
group null |
|||
public: |
|||
function operation$ { |
|||
if not .delegate is .null then |
|||
ret$=.delegate=>operation$(![]) ' [] is the stack of values (leave empty stack), ! used to place this to callee stack |
|||
else |
|||
ret$= "Default implementation" |
|||
end if |
|||
\\ a global variable and all group members except arrays use <= not =. Simple = used for declaring local variables |
|||
doc$<=ret$+{ |
|||
} |
|||
=ret$ |
|||
} |
|||
class: |
|||
Module Delegator { |
|||
class none {} |
|||
.null->none() |
|||
If match("G") then .delegate->(group) else .delegate<=.null |
|||
} |
|||
} |
|||
Class Thing { |
|||
function operation$(a,b) { |
|||
="Delegate implementation:"+str$(a*b) |
|||
} |
|||
} |
|||
Module CallbyReference (&z as group) { |
|||
Print Z.operation$(5,30) |
|||
} |
|||
Module CallbyValue (z as group) { |
|||
Print Z.operation$(2,30) |
|||
} |
|||
Module CallbyReference2 (&z as pointer) { |
|||
Print Z=>operation$(5,30) |
|||
} |
|||
Module CallbyValue2 (z as pointer) { |
|||
Print Z=>operation$(2,30) |
|||
} |
|||
\\ using named groups (A is a group, erased when this module exit) |
|||
A=Delegator() |
|||
B=Delegator(Thing()) |
|||
Print A.operation$(10,20) |
|||
Print B.operation$(10,20) |
|||
A=B |
|||
CallbyReference &A |
|||
CallbyValue A |
|||
\\ M2000 has two kinds of pointers to groups |
|||
\\ one is a pointer to a no named group (a float group) |
|||
\\ a float group leave until no pointer refer to it |
|||
\\ using pointers to groups (A1 is a pointer to Group) |
|||
A1->Delegator() |
|||
B1->Delegator(Thing()) |
|||
Print A1=>operation$(10,20) |
|||
Print B1=>operation$(10,20) |
|||
A1=B1 |
|||
CallbyReference2 &A1 |
|||
CallbyValue2 A1 |
|||
\\ Second type is a pointer to a named group |
|||
\\ the pointer hold a weak reference to named group |
|||
\\ so a returned pointer of thid kind can be invalid if actual reference not exist |
|||
A=Delegator() ' copy a float group to A |
|||
A1->A |
|||
B1->B |
|||
Print A1=>operation$(10,20) |
|||
Print B1=>operation$(10,20) |
|||
A1=B1 |
|||
CallbyReference2 &A1 |
|||
CallbyValue2 A1 |
|||
Report Doc$ |
|||
Clipboard Doc$ |
|||
} |
} |
||
Version2 |
|||
Class Thing { |
|||
function operation$ { |
|||
="Delegate implementation" |
|||
} |
|||
} |
|||
A=Delegator() |
|||
B=Delegator(Thing()) |
|||
Print A.operation$() ' default implementation |
|||
Print B.operation$() ' delegate implementation |
|||
A=B |
|||
Print A.operation$() ' delegate implementation |
|||
</lang> |
</lang> |
||
[[Output]] |
|||
<pre> |
|||
Output: |
|||
Default implementation |
|||
Delegate implementation: 200 |
|||
Delegate implementation: 150 |
|||
Delegate implementation: 60 |
|||
Default implementation |
|||
Delegate implementation: 200 |
|||
Delegate implementation: 150 |
|||
Delegate implementation: 60 |
|||
Default implementation |
|||
Delegate implementation: 200 |
|||
Delegate implementation: 150 |
|||
Delegate implementation: 60 |
|||
</pre> |
|||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |