patching base classes
jecel at merlintec.com
jecel at merlintec.com
Wed Nov 17 16:49:53 UTC 1999
Do we need to be able to patch base classes? If we want to be able to
do things like
Object>>isHappyObject
^ false
MyClass>>isHappyObject
^ true
then we certainly need this capability. The alternative is the highly
reflexive
anObject isKindOf: MyClass
My experience is that this second option will always come back to bite
you later, so I am willing to put up with quite a lot in order to be
able to code in the first style. I will suppose that adding methods
to a class is enough for nearly all uses, and that adding instance
variables is not needed. You can always logically delete a method
from a given class by adding this method:
SomeClass>>methodToBeDeleted
^ super methodToBeDeleted
so I will only worry about adding stuff.
Here is a very crude implementation that would get the job done. I am
sure there are much better alternatives.
Let's create an object called "viewpoint", which is just a dictionary
that associates a normal dictionary (we will call it the base dictionary)
with an extension (which is also a dictionary). Add these methods:
at: key for: viewpoint ifAbsent: absentBlock
"finds the value associated with key in the receiver as seen
in the given viewpoint"
self at: key for: viewpoint whenFound: [:x | ^x].
^ self at: key ifAbsent: absentBlock
at: key for: viewpoint whenFound: returnBlock
"searches extensions for key"
viewpoint at: self ifPresent: [ :extension |
extension at: key ifPresent: [ :answer |
returnBlock value: answer ]].
(viewpoint at: #imports) do: [ :vp |
self at: key for: vp whenFound: returnBlock ]
Each packages could have its own viewpoint (which could "import" extensions
from a number of other packages/viewpoints). If we modified the VM so
that a message lookup would see the various method dictionaries from
the viewpoint of the *sender*, the we would have the kind of dynamic
additions that Marcel Weiher wanted. Note that it wouldn't be a good
idea to have a global message cache anymore since the same message for
the same receiver could have different results. On the other hand, having
one cache for each viewpoint certainly doesn't seem a good idea since
they would be almost exactly the same. I don't have a good solution for
this.
Globals could also be handled by this same mechanism by always looking
up in Smalltalk using the current viewpoint. But I like replacing globals
with classes better.
-- Jecel
More information about the Squeak-dev
mailing list
|