Respond to an unknown method call: Difference between revisions
Content added Content deleted
(+ F#/Omit) |
(Added Logtalk example.) |
||
Line 390: | Line 390: | ||
example.ding("dong"); // alerts "tried to handle unknown method ding" |
example.ding("dong"); // alerts "tried to handle unknown method ding" |
||
// alerts "it had arguments: dong</lang> |
// alerts "it had arguments: dong</lang> |
||
=={{header|Logtalk}}== |
|||
There are two ways to handle unknown messages. From the sender side, we can catch the exception that is generated when an object doesn't understand a message: |
|||
<lang logtalk> |
|||
:- object(foo). |
|||
:- public(try/0). |
|||
try :- |
|||
catch(bar::message, Error, handler(Error)). |
|||
handler(error(existence_error(predicate_declaration,message/0),_)) :- |
|||
% handle the unknown message |
|||
... |
|||
:- end_object. |
|||
</lang> |
|||
From the receiver side, an object can implement the built-in protocol "forwarding", defining the handler for messages that it doesn't understands (e.g. by forwarding or delegating it to another object): |
|||
<lang logtalk> |
|||
:- object(foo, |
|||
implements(forwarding)). |
|||
forward(Message) :- |
|||
% handle the unknown message |
|||
... |
|||
:- end_object. |
|||
</lang> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |