Instance-specific behavior question (like Ruby?)

Bert Freudenberg bert at impara.de
Tue Mar 23 11:04:32 UTC 2004


Am 23.03.2004 um 09:00 schrieb Poster at Kinetium dot Com:

> I am somewhat familiar with the idiom of doing instance specialization 
> by
> overriding "doesNotUnderstand".
>
> Ruby provides a different and more direct way of doing such prototype 
> based
> programming: it simply makes the instance belong to a new (anonymous) 
> class,
> and puts any instance-specific behavior on that class. It provides a 
> syntax
> to define that behavior (since the class is anonymous).
>
> Is there a way to do this in Squeak? I'm a relative Squeak newbie ...

Yes, this is how the instance-specific methods in the Etoys environment 
are realized. Objects start out as instances of UnscriptedPlayer. As 
soon as you add a script, the object is transformed (see 
#assureUniClass) int a unique subclass of Player, and the script is 
added to this instance.

There is no special syntax for it, but you have to do a bit of "meta 
programming". You send #instanceOfUniqueClass to any class to get an 
instance of, you guessed it, a unique subclass. Then you can compile 
any method into this instances class, and have high-performance 
instance-specific behaviour.

- Bert -




More information about the Squeak-dev mailing list