[Newbies] Symbols, my nightmare
ramon.leon at allresnet.com
Fri Dec 28 19:52:53 UTC 2007
> I've got a question about this exchange, about symbols. Why
> do we use symbols? What are they for?
Think of it as a unique marker for making lookups fast using ==. The two
strings 'hello' and 'hello' aren't always == (the same instance) but the two
symbols #hello and #hello are.
> 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].
No, [asMorph] is not valid and will simply refer to an unbound local
variable named 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.
Because selectors are symbols, look at aClass methodDict and you'll see a
class keeps its methods in a dictionary keyed by the symbol that is used for
> 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.
It's on the instance side, so you have to send it to instances.
someObj := Object new.
> If I write "Object respondsTo:
> #asMorph", then I'm saying "Hello, Object, do you happen to
> have a method called asMorph?".
No you're not, you're saying hello "Object class", do you happen to have a
method called asMorph?
Object new asMorph
Talking to the class is different than talking to an instance of that class.
> (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
That's mostly true, but not all object inherit from object, some inherit
> But what does Ralph Johnson mean that you send #asMorph, a
> symbol for a method, only to an instance? And not a class?
He's trying to get you to realize that though classes are objects too,
talking to a class is different than talking to an instance of that class.
When you say Object respondsTo: #asMorph, you're asking Objects meta-class
if it supports that method, you're not asking if instances of Object
responds to it, for that, you need to create an instance, and then ask it,
i.e. Object new respondsTo: #asMorph is asking an instance of object, rather
than Object's meta-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?
Symbols are used to lookup a method in an objects method dictionary, the
symbol itself does not implement any behavior for the method. You don't
create sybmols for methods, symbols are just unique identifiers used for
> Any help would be greatly appreciated,
Hope this helps.
More information about the Beginners