Performing a method on only those objects which will understand it

Jim Rosenberg jr at amanue.com
Mon Jan 21 02:26:54 UTC 2002


Please pardon the Smalltalk 101ish nature of this question.

I'm developing some morphs that have various behaviors that I need for some 
interactive content.  In the process of doing this, I'm evolving some 
selectors that "my" morphs understand, but a generic morph might not 
understand.  I need a way to perform such a method on "my" morphs without 
causing an error if the class does not implement the method.

So far I've turned up the following as the way to do this.  In this example 
there is a temporary variable, parent, which is assigned an object.  I want 
to perform my method isPlaying: on that object if it understands this 
method, otherwise parent should do nothing.

    | parent |

    ...

  	 ((parent class) whichClassIncludesSelector: #isPlaying:) ifNotNil: [
		       parent isPlaying: true
  	 ].

Is this the way this is "supposed to be" done?  Is there a Better Way (TM)? 
This way seems reasonably straightforward, but I thought I'd ask before I 
get to many of these things scattered all over my code.

---
 Jim Rosenberg                      http://www.well.com/user/jer/
     WELL: jer
     Internet: jr at amanue.com




More information about the Squeak-dev mailing list