Anonymous user
Delegates: Difference between revisions
m
Fixed lang tags.
No edit summary |
m (Fixed lang tags.) |
||
Line 16:
=={{header|Ada}}==
All that is needed in order to implement this is a common base type. The delegator holds a pointer to an "untyped" object from the base class. Querying if the target implements the delegate interface is done using run-time type identification.
<lang ada>with Ada.Text_IO; use Ada.Text_IO;
procedure Delegation is
Line 64 ⟶ 63:
A.Delegate := Has_Thing'Access; -- Set a thing
Put_Line (A.Operation);
end Delegation;</lang>
</lang>▼
Sample output:
<pre>
Line 114 ⟶ 112:
This example uses tango for output.
<lang D>import tango.io.Stdout;
class Delegator
Line 143 ⟶ 140:
Stdout ( dr.setDg(thing).operation ).newline;
return 0;
▲}</lang>
=={{header|E}}==
▲ > delegator.operation()
▲ # value: "default implementation"
▲ ? def delegator := makeDelegator(def doesNotImplement {})
▲ > delegator.operation()
▲ # value: "default implementation"
▲ ? def delegator := makeDelegator(def doesImplement {
▲ > to thing() { return "delegate implementation" }
▲ > })
▲ > delegator.operation()
▲ # value: "default implementation"
=={{header|Java}}==
Line 327 ⟶ 322:
=={{header|Oz}}==
{{trans|Python}}
<lang
import
Open Application Module
Line 463 ⟶ 458:
=={{header|Pop11}}==
<lang pop11>uses objectclass;▼
▲uses objectclass;
define :class Delegator;
slot delegate = false;
Line 495 ⟶ 489:
;;; delegating to a freshly created Delegate
newDelegate() -> delegate(a);
operation(a) =></lang>
=={{header|Python}}==
Line 620 ⟶ 612:
To code the <code>operation</code> method without relying on catching an exception, but strictly by using introspection:
<lang tcl>
=={{header|Vorpal}}==
Delegate objects can be an array of delegates or as a single delegate.
<lang vorpal>a = new()
a.f = method(){
.x.print()
Line 657 ⟶ 648:
d.delegate = a
d.x = 7
d.f()</lang>
The resulting output:
|