Anonymous user
Respond to an unknown method call: Difference between revisions
Respond to an unknown method call (view source)
Revision as of 09:56, 14 October 2013
, 10 years agoAdded Logtalk example.
(+ F#/Omit) |
(Added Logtalk example.) |
||
Line 390:
example.ding("dong"); // alerts "tried to handle unknown method ding"
// 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}}==
|