[Newbies] Symbols, my nightmare

Diego Fernández diegof79 at gmail.com
Fri Dec 28 21:46:34 UTC 2007


Hi Chris, my english is not so good to explain complex things like  
this, but I'll try to do my best:

Objects responds to Messages, that is:
anObject message: arg1.

When you send a Message to an object, the message has:
- An identifier (the Selector) in this case: message:
- Arguments, in this case: arg1

Then anObject has to do something to respond that message.
So there is a dictionary that associates the message Selector with  
code that describes what to do. This code is the Method.
For implementation reasons, instead of using a String for the  
Selector, Smalltalk uses a Symbol. A Symbol is a special kind of  
String that represents an identifier.

Well the problem now is: how anObject comes to existence in first place?
Smalltalk is a OO language with classification, that is: there is  
objects called Classes who describes another objects (they are like  
templates or blueprints of objects).
They describe: the Selector-Methods that the object has and how the  
internal state of the object is stored (the instance variables).

Each Object has only one Class, so classes have a more strong meaning  
than a "template" for objects: they made an exclusive classification  
of objects, hence the name "Class".

In Smalltalk all objects responds to the message: class. Inspect:
1 class
and you will get SmallInteger the class for the object 1

Classes arises an interesting problem: if Classes are objects, which  
is the class of a Class?
Inspect:
1 class ---> SmallInteger
1 class class ---> SmallInteger class (an instance of Metaclass)
1 class class class --> Metaclass
1 class class class class --> Metaclass class  (an instance of  
Metaclass)

So this infinite recursion problem is solved with a base case:  
Metaclass. Is not elegant but it works.

Back to your question, you could try to inspect:
1 to: 2

Then browse SmallInteger, and look for the method with selector #to: ...

Is not there!
That is because #to: is defined in the class Number.
When the object 1, receives the Message with the Selector #to:  VM  
first looks at SmallInteger, then Integer, then Number where the  
method #to: is defined. (this process is called method lookup).

When you write:
1 respondsTo:  #to:
The true/false response tells if this object (1) can respond to a  
message identified by #to: (which is a symbol because the selectors  
used to identify methods are symbols).
If you evaluate:
Number respondsTo: #to:
you will get false because you are asking to the Number class which is  
another object, that describes a method #to: but doesn't responds to a  
method identified with #to:

You get true when you do: Object respondsTo: #asMorph
Because Object is an instance of the Metaclass: Object class.
Which is a subclass of ClassDescription, which is a subclass of  
Behavior, which is a subclass of Object, an all instances of objects  
responds to #asMorph!!
Pretty weird :)

In a few words:
1 respondsTo: #msg (tells if the object 1 can handle the message  
identified with the symbol #msg)

SmallInteger canUnderstand: #msg (tells if there is a definition for a  
method identified with #msg)

SmallInteger methodDictionary (inspect it to see the methods defined  
for each selector)

Symbol, Selector, Message, and Method are different things, but...  
when we talk usually we use Selector/Message/Method interchangeably.


On Dec 28, 2007, at 4:23 PM, Chris Cunnington wrote:

>
>
> 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
>
>
>
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/beginners/attachments/20071228/7f8cfb9c/attachment-0001.htm


More information about the Beginners mailing list