Check this out:
ErrorMgr errors = ErrorMgr(); // answers error() Vehicle.delegate(errors); Car c = Car(); // car does not answer error() c.error("we crashed");
here I am calling the delegate method on the Vehicle class, of which Car is a subclass. I make an instance of a car and then send it the error message, which is forwarded to the error manager. Here are the other definitions:
class Vehicle { string color; string brand; } class Car extends Vehicle { float mpg; int numDoors; } class ErrorMgr { error(string msg) { println("error: "+msg); } }
This is a delegation scheme per class not per instance. All instances of Vehicle share the same delegate, but subclasses can override. If there is no delegate for an object's class, the message is forwarded up the class inheritance hierarchy. So, for example, you could add a delegate to object that all instances in your program could access my method.
I'm now working on a dynamic mixin facility, which differs from a dynamic delegation in that the mixin knows which object forwarded the message to it. The 'this' pointer of the delegate must be the usual whereas a mixin is assumed to have a 'this' pointer of the delegator.
By the way, I'm going back to calling mantra's mixins "mixins". Use "include" to bring in methods:
class int includes Comparable {...} // statically mixin Comparable's methods mixin Comparable {...}
Now I just have to update the doc to include all of the changes I've just made. I even added a meta object protocol, the natural place to store delegates per class. Getting verrrry close now.