^self question name
Now, I'm a lazy developer. I didn't want to write these messages everytime I add some new messages to question that I want my response to asnwer. So, what do I do? I could implement #doesNotUnderstand so that it forwards the message, but I didn't want to do that because:
- It's hard to debug
- It's S-S-L-L-O-O-W-W
So, what do you do? Well, in Smalltalk, I can have the #doesNotUnderstand: message basically compile a new method for me because it's always of the form listed above. This gives me the performance and it's easy to debug. If you put these special methods in their own category then you can easily track them. Tracking them is good to catch subtle forwarding bugs. Here's what the #doesNotUnderstand: message looks like:
(self definition respondsTo: aMessage selector)
[self compile: aMessage selector forwarder: #definition.
^aMessage sendTo: self].
^super doesNotUnderstand: aMessage
And here's what the compile code looks like:
compile: selector forwarder: forwarderSelector
| messageStream header |
messageStream := String new writeStream.
header := (self perform: forwarderSelector) class methodHeaderFor: selector.
nextPutAll: '^self ';
nextPut: Character space;
self class compile: messageStream contents classified: self class autoGeneratedCategory
And that's it! This allows me to have a form of delegation by forwarding messages to certain objects. Other objects now do not have to grab an object just to perform actions on it. This makes your objects more shy which is a good thing. Micheal liked the idea and asked me why I had not blogged about it. I told him I didn't think much about it. It's just so simple. He told me that I should blog about it. Well, here it is...=)