<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hi Chris, my english is not so good to explain complex things like this, but I'll try to do my best:<div><br class="webkit-block-placeholder"></div><div>Objects responds to Messages, that is:</div><div>anObject message: arg1.</div><div><br class="webkit-block-placeholder"></div><div>When you send a Message to an object, the message has:</div><div>- An identifier (the Selector) in this case: <i>message:</i></div><div>- Arguments, in this case: <i>arg1</i>&nbsp;</div><div><br></div><div>Then anObject has to do something to respond that message.&nbsp;</div><div>So there is a dictionary that associates the message Selector with code that describes what to do. This code is the Method.</div><div>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.</div><div>&nbsp;</div><div>Well the problem now is: how anObject comes to existence in first place?</div><div>Smalltalk is a OO language with classification, that is: there is objects called Classes who&nbsp;describes another objects (they are like templates or blueprints of objects).</div><div>They describe: the Selector-Methods that the object has and how the internal state of the object is stored (the instance variables).</div><div><br class="webkit-block-placeholder"></div><div>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".</div><div><br class="webkit-block-placeholder"></div><div>In Smalltalk all objects responds to the message: class. Inspect:&nbsp;</div><div>1 class&nbsp;</div><div>and you will get SmallInteger the class for the object 1</div><div><br class="webkit-block-placeholder"></div><div>Classes arises an interesting problem: if Classes are objects, which is the class of a Class?</div><div>Inspect:</div><div>1 class ---&gt; SmallInteger</div><div>1 class class ---&gt; SmallInteger class (an instance of Metaclass)</div><div>1 class class class --&gt; Metaclass</div><div>1 class class class class --&gt; Metaclass class &nbsp;(an instance of Metaclass)</div><div><br class="webkit-block-placeholder"></div><div>So this infinite recursion problem is solved with a base case: Metaclass. Is not elegant but it works.</div><div><br></div><div>Back to your question, you could try to inspect:</div><div>1 to: 2</div><div><br class="webkit-block-placeholder"></div><div>Then browse SmallInteger, and look for the method with selector #to: ...&nbsp;</div><div><br class="webkit-block-placeholder"></div><div>Is not there!</div><div>That is because #to: is defined in the class Number.</div><div>When the object 1, receives the Message with the Selector #to: &nbsp;VM first looks at SmallInteger, then Integer, then Number where the method #to: is defined. (this process is called method lookup).</div><div><br class="webkit-block-placeholder"></div><div>When you write:</div><div>1 respondsTo: &nbsp;#to:</div><div>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).</div><div>If you evaluate:&nbsp;</div><div>Number respondsTo: #to:</div><div>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:</div><div><br class="webkit-block-placeholder"></div><div>You get true when you do: Object respondsTo: #asMorph</div><div>Because Object is an instance of the Metaclass: Object class.</div><div>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!!&nbsp;</div><div>Pretty weird :)</div><div><br class="webkit-block-placeholder"></div><div>In a few words:</div><div>1 respondsTo: #msg (tells if the object 1 can handle the message identified with the symbol #msg)</div><div><br class="webkit-block-placeholder"></div><div>SmallInteger canUnderstand: #msg (tells if there is a definition for a method identified with #msg)</div><div><br class="webkit-block-placeholder"></div><div>SmallInteger methodDictionary (inspect it to see the methods defined for each selector)</div><div><br class="webkit-block-placeholder"></div><div>Symbol, Selector, Message, and Method are different things, but... when we talk usually we use Selector/Message/Method&nbsp;interchangeably.</div><div><br class="webkit-block-placeholder"></div><div><br class="webkit-block-placeholder"></div><div>On Dec 28, 2007, at 4:23 PM, Chris Cunnington wrote:</div><div><div><br class="Apple-interchange-newline"><blockquote type="cite"><br><br>Me: <br><blockquote type="cite">I tried this:<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">Object respondsTo: asMorph<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">I print it, and get:<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">Object respondsTo: asMorph false<br></blockquote><blockquote type="cite"><br></blockquote><br>Ralph Johnson:<br><blockquote type="cite">It should be "Object respondsTo: #asMorph". &nbsp;But in fact, you can say<br></blockquote><blockquote type="cite">"3 respondsTo: #asMorph". &nbsp;The reason that the first works is that<br></blockquote><blockquote type="cite">Object is an instance of a subclass of Object. &nbsp;&nbsp;&nbsp;#asMorph is a<br></blockquote><blockquote type="cite">message you send to instances, not classes.<br></blockquote><br>I've got a question about this exchange, about symbols. Why do we use<br>symbols? What are they for?<br><br>I know that all class names are symbols. We define them in the template with<br>a hash mark, and then the class name has immediate global scope. We can call<br>it from anywhere. Also, we only need to hash mark when we declare a symbol.<br>I can call the name of a class from anywhere without using the hash mark.<br><br>I know that sometimes a programmer will use a symbol as an argument. Above,<br>my use of Object calls a variable called asMorph. Ralph Johnson adds the<br>symbol, and now the argument is referring to a method called asMorph.<br><br>Does that mean people have a choice between a symbol and a block? If I want<br>the selector to call another method, it seems I could write #asMorph &nbsp;or<br>[asMorph]. <br><br>And why do people on this message board refer to the selectors in question<br>as though they were all symbols? (i.e. #respondsTo:) respondsTo: is a<br>selector, not a symbol. Is that just some kind of convention, because it's<br>not what the code is doing. respondsTo: is a selector, not any symbol... I<br>don't think. <br><br>The kicker. Ralph Johnson says: "#asMorph is a message you send to<br>instances, not classes". What? AsMorph is a method. A method of the class<br>Object. If I write "Object respondsTo: #asMorph", then I'm saying "Hello,<br>Object, do you happen to have a method called asMorph?". (Object makes this<br>a bit confusing, I think, because you can ask any class if they have method<br>asMorph, and it will be true, because any class can find asMorph by asking<br>Object, which is at the top of the hierarchy.)<br><br>But what does Ralph Johnson mean that you send #asMorph, a symbol for a<br>method, only to an instance? And not a class?<br><br>And finally, if you create a symbol for a method, then doesn't that give<br>that symbol global scope, as it did with the defining of a class? And<br>wouldn't that undermine polymorphism? There are more than half a dozen<br>classes that use the method asMorph. I should say, there are nine<br>implemention of asMorph. Each defined in its own way. If a symbol has<br>universal scope, then how does the system chose one of those nine? How do<br>you limit the scope of a symbol?<br><br>Any help would be greatly appreciated,<br><br>Chris <br><br><br><br><br><br><br>_______________________________________________<br>Beginners mailing list<br><a href="mailto:Beginners@lists.squeakfoundation.org">Beginners@lists.squeakfoundation.org</a><br>http://lists.squeakfoundation.org/mailman/listinfo/beginners<br></blockquote></div><br></div></body></html>