[Newbies] Symbols, my nightmare

Chris Cunnington cunnington at sympatico.ca
Fri Dec 28 19:23:02 UTC 2007



Me: 
> I tried this:
>
> Object respondsTo: asMorph
>
> I print it, and get:
>
> Object respondsTo: asMorph false
>

Ralph Johnson:
>It should be "Object respondsTo: #asMorph".  But in fact, you can say
>"3 respondsTo: #asMorph".  The reason that the first works is that
>Object is an instance of a subclass of Object.    #asMorph is a
>message you send to instances, not classes.

I've got a question about this exchange, about symbols. Why do we use
symbols? What are they for?

I know that all class names are symbols. We define them in the template with
a hash mark, and then the class name has immediate global scope. We can call
it from anywhere. Also, we only need to hash mark when we declare a symbol.
I can call the name of a class from anywhere without using the hash mark.

I know that sometimes a programmer will use a symbol as an argument. Above,
my use of Object calls a variable called asMorph. Ralph Johnson adds the
symbol, and now the argument is referring to a method called asMorph.

Does that mean people have a choice between a symbol and a block? If I want
the selector to call another method, it seems I could write #asMorph  or
[asMorph]. 

And why do people on this message board refer to the selectors in question
as though they were all symbols? (i.e. #respondsTo:) respondsTo: is a
selector, not a symbol. Is that just some kind of convention, because it's
not what the code is doing. respondsTo: is a selector, not any symbol... I
don't think. 

The kicker. Ralph Johnson says: "#asMorph is a message you send to
instances, not classes". What? AsMorph is a method. A method of the class
Object. If I write "Object respondsTo: #asMorph", then I'm saying "Hello,
Object, do you happen to have a method called asMorph?". (Object makes this
a bit confusing, I think, because you can ask any class if they have method
asMorph, and it will be true, because any class can find asMorph by asking
Object, which is at the top of the hierarchy.)

But what does Ralph Johnson mean that you send #asMorph, a symbol for a
method, only to an instance? And not a class?

And finally, if you create a symbol for a method, then doesn't that give
that symbol global scope, as it did with the defining of a class? And
wouldn't that undermine polymorphism? There are more than half a dozen
classes that use the method asMorph. I should say, there are nine
implemention of asMorph. Each defined in its own way. If a symbol has
universal scope, then how does the system chose one of those nine? How do
you limit the scope of a symbol?

Any help would be greatly appreciated,

Chris 








More information about the Beginners mailing list