Generics

Avi Bryant avi at beta4.com
Fri Oct 3 17:10:05 UTC 2003


On Fri, 3 Oct 2003, Phil Hudson wrote:

> I do have a question though. Look what happens:
>
> anIhoTypesafeBag isKindOf: Bag.  "answers false"
>
> Now this is correct from the strict VM and class hierarchy point of view,
> but false from the user's point of view. Did you consider overriding
> #isKindOf: and decide against? If not, would it be right or wrong to write:
>
> IhoTypesafeBag>>isKindOf: aClass
>     ^ bag isKindOf: aClass
>
> Would that break anything important that already exists? Please let me
> know if I've misunderstood the semantics of #isKindOf: - I take it to be
> cognate with Java's 'instanceOf' operator.

It is cognate with instanceof, but that's a low level operation and should
return a low level result.  You should only be using #isKindOf: in the
rare cases where you truly want to know which class an object has - in
which case you would want to be able to tell the difference between a Bag
and a TypesafeBag.  In most cases, however, what you would want to do is
send the object a message like #isBag - which is probably closer to asking
an object in Java if it implements a particular interface (see the comment
for #isCollection, for example).  There doesn't happen to be an #isBag in
the image, which means that nobody has found it terribly useful to
determine if an object is Bag-like yet, but it could easily be added.
Overriding this method to return true would be your way of indicating "for
all practical purposes, treat this object as a bag".

Hopefully this won't degenerate into the lengthy discussion we had on this
list a while ago about the suitability of having #isFoo messages on
Object, etc...

Avi



More information about the Squeak-dev mailing list