Delegates: Difference between revisions

Content added Content deleted
(Add C)
(→‎{{header|Oz}}: simplified; made more idiomatic)
Line 411:
=={{header|Oz}}==
{{trans|Python}}
<lang oz>functordeclare
class Delegator from BaseObject
import
}attr
Open Application Module
delegate := DGunit
define
Stdout = {New Open.file init(name:stdout)}
 
meth set(DG)
%% modify from http://osdir.com/ml/lang.mozart.user/2007-04/msg00042.html
{Object.is DG} = true %% assert: DG must be an object
%% not known if this is a right way ...
delegate := DG
fun{IsMethod Klax Meth NArg}
B = {NewCell false}end
in
if {IsObject Klax} then
[BootObj BootName] = {Module.link ['x-oz://boot/Object' 'x-oz://boot/Name']}
MethList = {BootObj.getClass Klax}.{BootName.newUnique 'ooMeth'}
MethName = {Dictionary.keys MethList}
in
{For 1 {Length MethName} 1
proc{$ Idx}
if {Value.type MethName.Idx} == 'tuple' andthen
MethName.Idx.1 == Meth andthen
{Value.type {Dictionary.get MethList MethName.Idx.1}} == 'procedure' andthen
{ProcedureArity {Dictionary.get MethList MethName.Idx.1}} == NArg
then
B := true
end
end
}
end
@B
end
 
meth operation(?R$)
%% translate from Python version
if {IsMethod @delegate 'thing'== 1}unit then
class Delegator
{self default($)}
attr delegate
else
meth operation(?R)
@Btry
if {IsMethod @delegate 'thing' 1} then
{@delegate thing(R$)}
catch error(object(lookup ...) ...) then
else
%% the delegate did not understand the message
R = 'default implementation'
end{self default($)}
end
end
meth init
in end
delegate := {New BaseObject noop}
end
meth set(DG)
delegate := DG
end
end
 
meth initdefault($)
R = ' "default implementation' "
end
end
class Delegate from BaseObject
meth thing(?R$)
R = 'Delegate "delegate Implementation'"
end
end
{A set(= {New DelegateDelegator noop})}
in
{StdoutSystem.showInfo write(vs:{A operation($)}#'\n')}
 
{A = set({New DelegatorBaseObject initnoop})}
{StdoutSystem.showInfo write(vs:{A operation($)}#'\n')}
{A set('A delegate may be any object')}
{Stdout write(vs:{A operation($)}#'\n')}
{A set({New Delegate noop})}
{Stdout write(vs:{A operation($)}#'\n')}
 
{A set({New Delegate noop})}
{Application.exit 0}
{System.showInfo {A operation($)}}</lang>
end</lang>
 
=={{header|Perl}}==