Respond to an unknown method call: Difference between revisions
→{{header|Phix}}
Walterpachl (talk | contribs) (→{{header|ooRexx}}: corrected code) |
|||
Line 925:
$l.some-other-name = 42;
say $l.i-dont-know; # 42</lang>
=={{header|Phix}}==
Phix is not object orientated, but this sort of thing is very easy to emulate.
<lang Phix>enum METHODS
function invoke(object o, string name, sequence args={})
--(this works on any class, for any function, with any number or type of parameters)
integer mdict = o[METHODS]
integer node = getd_index(name,mdict)
if node!=0 then
return call_func(getd_by_index(node,mdict),args)
end if
return "no such method" -- or throw(), fatal(), etc
end function
--class X: Xmethods emulates a vtable
constant Xmethods = new_dict()
function exists()
return "exists"
end function
setd("exists",routine_id("exists"),Xmethods)
--class X: create new instances
function newX()
return {Xmethods}
end function
object x = newX()
?invoke(x,"exists")
?invoke(x,"non_existent_method")</lang>
{{out}}
<pre>
"exists"
"no such method"
</pre>
=={{header|PHP}}==
|