Performing a method on only those objects which will understand it

Richard A. O'Keefe ok at atlas.otago.ac.nz
Mon Jan 21 05:13:45 UTC 2002


While adding a method to Morphic is unpleasant, note that there's a lot of
this in Squeak already.  For example, Object>>costumes.  Does it really
make sense to ask _any_ object what its costumes are, without knowing
what the message might mean to the object, or what it might return?

For the specific question asked, Smalltalk has had for a long time
	Object>>respondsTo: aSymbol
	  ^ self class canUnderstand: aSymbol
so
    (thatMorph respondsTo: #myMessage) ifTrue:
	[thatMorph myMessage].
is about as clear as it get.

But that stinks.  It tacitly assumes that "do nothing" is the right
implementation of #myMessage: for all except a few classes, whereas
doesNotUnderstand is probably the right behaviour.  That is, using
this technique in effect switches off error detection; should there
be a mistake in the program such that thatMorph is not in fact a Morph
at all, it will not be noticed at all, or not until much later.

Bad idea.

Doing this will also mean that if a new kind of Morph that _should_
support this message accidentally fails to, that won't be noticed either.

Both of these objections also apply to adding a method to Object or Morph.

If the aim is to send a particular message to the owner of a morph,
if it understands that message, you could try putting it in the child.

    askOwnerToThink: n secondsAbout: topic
        (self owner respondsTo: #think:secondsAbout:) ifTrue:
	    [self owner think: n secondsAbout: topic].

That would work if all the child Morphs that you want to do this belong
to your classes.  It still has the problems mentioned above, but at least
it's in one place (per method in question).

What if X has owner Y has owner Z and Z is one of your classes and Y is
not?  Why not pass the message on to Z?  If you change your mind about
that, you definitely want the code encapsulated in one place.

It's probably best to turn the code inside out and have your morphs ask
for something instead of telling them.



More information about the Squeak-dev mailing list