Delegates: Difference between revisions
Content added Content deleted
Line 2,025: | Line 2,025: | ||
say "Delegate: "~$d.operation;</lang> |
say "Delegate: "~$d.operation;</lang> |
||
=={{header|Phix}}== |
|||
Phix is not object orientated, instead I would just use a routine_id for this sort of thing.<br> |
|||
I will admit that the whole concept of "no delegate/with one that does not implement" makes no sense whatsoever to me.<br> |
|||
While I've shown this using a single rid, you could of course hold an entire sequence of them or even better a dictionary and use named lookups for rids in that. |
|||
<lang Phix>enum OTHER, OPERATION |
|||
function operation(object o) |
|||
integer rid = o[OPERATION] |
|||
if rid!=NULL then |
|||
return call_func(rid,{}) |
|||
end if |
|||
return "no implementation" |
|||
end function |
|||
function xthing() |
|||
return "default implementation" |
|||
end function |
|||
function newX() |
|||
return {1,routine_id("xthing"),2} |
|||
end function |
|||
function newY() |
|||
object res = newX() |
|||
res[OTHER] = "something else" |
|||
-- remove delegate: |
|||
res[OPERATION] = NULL |
|||
return res |
|||
end function |
|||
function zthing() |
|||
return "delegate implementation" |
|||
end function |
|||
function newZ() |
|||
object res = newX() |
|||
-- replace delegate: |
|||
res[OPERATION] = routine_id("zthing") |
|||
return res |
|||
end function |
|||
object x = newX(), |
|||
y = newY(), |
|||
z = newZ() |
|||
?operation(x) |
|||
?operation(y) |
|||
?operation(z)</lang> |
|||
{{out}} |
|||
<pre> |
|||
"default implementation" |
|||
"no implementation" |
|||
"delegate implementation" |
|||
</pre> |
|||
Obviously, you can explictly test for rid=NULL as shown, or remove that test and catch exceptions, ie: |
|||
<lang Phix>?operation(x) |
|||
try -- (since rid=NULL check commented out) |
|||
?operation(y) |
|||
catch e |
|||
?"oops, no implementation" |
|||
end try |
|||
?operation(z)</lang> |
|||
=={{header|PHP}}== |
=={{header|PHP}}== |