adding methods to instances

Jim Menard jimm at io.com
Wed Apr 11 19:43:47 UTC 2001


Ned Konz writes:
> On Wednesday 11 April 2001 12:30, Karl Ramberg wrote:
> > Why does not Smalltalk allow adding methods to instances ?
> 
> Because the method lookup is done in a class-based methodDictionary?
> 
> You could make an anonymous copy of a class and replace one or more 
> CompiledMethods in its methodDictionary (I suspect, having done this 
> successfully in VW) then use that class to make one or more instances that 
> would have the new behavior.
> 
> But the tools don't currently support that kind of subterfuge.
> 
> I don't know whether you can change the class pointer of an object at runtime.

Great ideas. How about using delegation and #doesNotUnderstand: instead?

Add an instance variable to object called delegate. Override
Object>>doesNotUnderstand: to pass the message to delegate, if it is not
nil. Perhaps provide a setter method or two. Add a new delegate object to
your object and voila! new methods.

Jim
-- 
Jim Menard, jimm at io.com, http://www.io.com/~jimm/
"Do Squeakers Dream of Electric Mice?" -- Mike Thomas
    (http://www.squeak.org)





More information about the Squeak-dev mailing list