Question

It seems that the inheritsFrom: method in GNU Smalltalk returns true for every undefined class name sent to it as a parameter. This might make a program very hard to debug, IMHO. Looking at the code for this in the Behavior class, it looks like this:

inheritsFrom: aClass [
"Returns true if aClass is a superclass of the receiver"

<category: 'testing the class hierarchy'>
| sc |
aClass isNil ifTrue: [^true].

sc := self.
[sc := sc superclass.
sc isNil] whileFalse: [sc == aClass ifTrue: [^true]].
^false
]

The line aClass isNil ifTrue: [^true] is the culprit, but I am looking for a sane reason as to way it was coded this way. (I am new to the Smalltalk world, by the way, and trying to learn.)

Thanks.

Was it helpful?

Solution

This is not related to exceptions but rather to undefined variables. In GNU Smalltalk's REPL, or alternatively in a Transcript or Workspace for graphical environments, undefined variables are all nil---this is not very different in Ruby, for example.

So you are doing in fact "Object inheritsFrom: nil", which is true. I didn't know it is false in Squeak; that would be a departure from the Blue Book, actually.

In a method, you would have gotten a "variable fooobaar undefined" compile-time error.

OTHER TIPS

I suppose the thinking is that all class references must imply a class of some sort so the nil reference implies Object, which is the superclass of everything.

Squeak does it more the way you'd expect:

inheritsFrom: aClass 

|aSuperclass |
aSuperclass := superclass.
[aSuperclass == nil]
    whileFalse: [aSuperclass == aClass
            ifTrue: [^ true].
        aSuperclass := aSuperclass superclass].
^ false

link text

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top