Delegates: Difference between revisions
Content deleted Content added
Thundergnat (talk | contribs) Rename Perl 6 -> Raku, alphabetize, minor clean-up |
|||
Line 70: | Line 70: | ||
delegate implementation |
delegate implementation |
||
</pre> |
</pre> |
||
=={{header|Aikido}}== |
|||
<lang aikido> |
|||
class Delegator { |
|||
public generic delegate = none |
|||
public function operation { |
|||
if (typeof(delegate) == "none") { |
|||
return "default implementation" |
|||
} |
|||
return delegate() |
|||
} |
|||
} |
|||
function thing { |
|||
return "delegate implementation" |
|||
} |
|||
// default, no delegate |
|||
var d = new Delegator() |
|||
println (d.operation()) |
|||
// delegate |
|||
var d1 = new Delegator() |
|||
d1.delegate = thing |
|||
println (d1.operation()) |
|||
</lang> |
|||
=={{header|Aime}}== |
=={{header|Aime}}== |
||
Line 114: | Line 142: | ||
return 0; |
return 0; |
||
}</lang> |
}</lang> |
||
=={{header|Aikido}}== |
|||
<lang aikido> |
|||
class Delegator { |
|||
public generic delegate = none |
|||
public function operation { |
|||
if (typeof(delegate) == "none") { |
|||
return "default implementation" |
|||
} |
|||
return delegate() |
|||
} |
|||
} |
|||
function thing { |
|||
return "delegate implementation" |
|||
} |
|||
// default, no delegate |
|||
var d = new Delegator() |
|||
println (d.operation()) |
|||
// delegate |
|||
var d1 = new Delegator() |
|||
d1.delegate = thing |
|||
println (d1.operation()) |
|||
</lang> |
|||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Line 445: | Line 445: | ||
} |
} |
||
</lang> |
</lang> |
||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
<lang clojure>(defprotocol Thing |
<lang clojure>(defprotocol Thing |
||
Line 751: | Line 752: | ||
> delegator.operation() |
> delegator.operation() |
||
# value: "default implementation"</lang> |
# value: "default implementation"</lang> |
||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
Line 2,039: | Line 2,039: | ||
</lang> |
</lang> |
||
=={{header|Perl 6}}== |
|||
<lang perl6>class Non-Delegate { } |
|||
class Delegate { |
|||
method thing { |
|||
return "delegate implementation" |
|||
} |
|||
} |
|||
class Delegator { |
|||
has $.delegate is rw; |
|||
method operation { |
|||
$.delegate.^can( 'thing' ) ?? $.delegate.thing |
|||
!! "default implementation" |
|||
} |
|||
} |
|||
my Delegator $d .= new; |
|||
say "empty: "~$d.operation; |
|||
$d.delegate = Non-Delegate.new; |
|||
say "Non-Delegate: "~$d.operation; |
|||
$d.delegate = Delegate.new; |
|||
say "Delegate: "~$d.operation;</lang> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 2,306: | Line 2,276: | ||
All the tests pass. Believe me. |
All the tests pass. Believe me. |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
<lang perl6>class Non-Delegate { } |
|||
class Delegate { |
|||
method thing { |
|||
return "delegate implementation" |
|||
} |
|||
} |
|||
class Delegator { |
|||
has $.delegate is rw; |
|||
method operation { |
|||
$.delegate.^can( 'thing' ) ?? $.delegate.thing |
|||
!! "default implementation" |
|||
} |
|||
} |
|||
my Delegator $d .= new; |
|||
say "empty: "~$d.operation; |
|||
$d.delegate = Non-Delegate.new; |
|||
say "Non-Delegate: "~$d.operation; |
|||
$d.delegate = Delegate.new; |
|||
say "Delegate: "~$d.operation;</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
Line 2,479: | Line 2,480: | ||
}</lang> |
}</lang> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
<lang ruby>class NonDelegate { } |
<lang ruby>class NonDelegate { } |