Object: Identity vs. Environment

Lothar Schenk lothar.schenk at gmx.de
Tue Jun 3 18:15:24 UTC 2003


Richard A. O'Keefe wrote:

> Joel Shellman <joel at ikestrel.com> replied to my message:
> 	> But *some* methods really can be given *anything*,
> 	> and for those methods it is perfectly legitimate to put the
> 	> discriminations they need in Object.
>
> 	Please give an example. If they have to make distinctions, than that means
> 	they're not able to accept *anything*.
>
> That does not follow.  In fact, it is *because* they make distinctions
> that they *can* accept anything.
>
> A good example is the method Object>>isColor.
> It's used in
>
>     Color>>
>     = aColor
>         aColor isColor ifFalse: [^false].
>         ^aColor privateRGB = rbg and: [
>             aColor privateAlpha = self privateAlpha]
>
> This method accepts ANY OBJECT WHATSOEVER as its argument.
> It is only because it makes the distinction "is this a colour or not"
> that it is able to do so.  For things that answer true to #isColor,
> it checks their privateRGB and privateAlpha methods; for things that
> answer false to #isColor it immediately returns false.

[snip]

> In any case, there is no way of testing for a protocol in Smalltalk
> other than sending an #isFoo message.

I agree with you in principle, but there is still something wrong here. Object 
has no business knowing anything about Fooness, and so it shouldn't provide a 
method to test specifically for Fooness, even if it is only lexically 
suggested that it do so. It is nevertheless true that all objects should have 
a generic method for checking if the particular instance in question has 
Fooness, but not only specifically for Fooness, but also for any other 
criterion of belonging to a particular set.

The keyword here is parameterization.

So why not do it this way:

Object>>fulfills: aCriterion

              ^false

Foo>>fulfills: aCriterion

              ^ (aCriterion = #Fooness)
             "Note that one can just as well react to more than one criterion"

Color>>fulfills: aCriterion

              ^ (aCriterion = #Colorness)

This is similar to the case when I would ask you if you were a Kroogleswank. 
If you are not a Kroogleswank you needn't even know what a Kroogleswank is to 
reply that you are not.

So, your example above would be written the following way (using #Colorness as 
the criterion for things which behave like colors):

Color>> = aColor

              (aColor fulfills: #Colorness) ifFalse: [^false].
              ^aColor privateRGB = rbg and: [
                aColor privateAlpha = self privateAlpha]

Apparently, this is no more complex than it was before, but this version has 
the advantage that only those classes that either provide or use (check for) 
Fooness need to know anything about it, while Object itself never needs to 
know anything about Fooness, Colorness or whatever, as it should be.

And we can scrap all the myriad isThis and isThatOrOther in Object for one 
simple generic method.

-- Lothar

"Walk this world with me"




More information about the Squeak-dev mailing list