Hacker News new | ask | show | jobs
by panic 3293 days ago
Here's a concrete example: say I want to write a class which wraps an object, logging all the methods called on it. So if I say (in C++-ish syntax):

    Dog *dog = new Dog()
    LoggingWrapper *wrapper = new LoggingWrapper(dog)
    wrapper->runAround()
The program should output a log entry like "Dog->runAround() called", then the dog should run around.

In Smalltalk, you can implement `LoggingWrapper doesNotUnderstand:` to output the log entry; something like:

    doesNotUnderstand: aMessage
        Logger log: target class, ' ', aMessage selector, ' called'.
        ^ aMessage sentTo: target.
It's not impossible to implement a class like LoggingWrapper using vtables -- in order to make a vtable, you have to know all a class's methods at compile time, but LoggingWrapper can respond to any method its target responds to.