Pharo Smalltalk - Message forwarding, is it possible to intercept a message and redirect it to another Object (instance)?
In Objective-C there's forwardInvocation: that gets called right before an exception is thrown so you can do something about the message you received and know nothing about.
Is there something similar in Smalltalk do I can redirect a message to a delegate?
Smalltalk has doesNotUnderstand: aMessage
which is sent to the receiver in place of an otherwise undefined method. You can override it and do whatever you wish (e.g. forward the message to another object, log it to disk, ...), for instance:
doesNotUnderstand: aMessage
aMessage sendTo: self delegate.
If you want to "intercept" messages which are actually defined on an object, you have two options:
- subclassing and using your own objects
- using method wrappers to replace the original method definitons which allows all kinds of manipulations (redirecting messages to new receivers, executing pre- and post-message-hooks, preventing the execution of the wrapped method, etc).