overridding without subclassing examples

Igor Stasenko siguctua at gmail.com
Wed Sep 19 06:05:45 UTC 2007


On 19/09/2007, Brad Fuller <bradallenfuller at yahoo.com> wrote:
> On Tue September 18 2007, Matthew Fulmer wrote:
> > On Tue, Sep 18, 2007 at 03:14:15PM -0700, Brad Fuller wrote:
> > > Is there a way to override an instance method w/o subclassing?
> > > For instance, class Morph has a method that returns true, but I want the
> > > that method to return false when instancing some types of Morphs and
> > > others not. (there is no setter/getter for this method.) Is there a way
> > > to do that w/o adding a setter/getter to Morph or subclassing the
> > > different Morphs?
> >
> > Your question is pretty vague
> >
> > I imagine you see something like
> >
> > isMorph
> >     ^ true
> >
> > in class morph, but you want some (or all?) morphs to instead
> > use
> >
> > isMorph
> >     ^ self isSomethingWeirdHappening not
> >
> > First you can redefine the method in the appropriate class(es). If you
> > want to put this change in a MC package, move it to category
> >
> > *MyPackage-override
> >
> > and this method will be stored in the package, and will override
> > the existing method when loaded.
> >
> > Package-level overrides are not recommended, but they are available
>
> But doesn't that just replace the method? I don't want to change the original
> class nor do I want to subclass.
>
>
You can do replacement in other way.
Suppose you have a class , which is a subclass of interesting class,
where you override method(s).
Then, at package initialization (or even before invoking some code,
where you need a replacement) you can do:

backupClass := Morph.
Smalltalk at: #Morph put: MyMorphWithOverrides.
.... run code ...

"restore original class"
Smalltalk at: #Morph put: backupClass.


-- 
Best regards,
Igor Stasenko AKA sig.



More information about the Squeak-dev mailing list