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 [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
> 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 selector. > 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. someObject asMorph > 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 asMorph vs 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 > hierarchy.) That's mostly true, but not all object inherit from object, some inherit from ProtoObject. > 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 efficient hashing. > > Any help would be greatly appreciated, > > Chris Hope this helps. Ramon Leon http://onsmalltalk.com _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
> Hope this helps.
> > Ramon Leon Thank you very much for your help. These answers shed a lot of light. Chris _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
In reply to this post by Chris Cunnington-5
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:
_______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
>>>>> "Diego" == Diego Fernández <[hidden email]> writes:
Diego> SmallInteger methodDictionary (inspect it to see the methods defined for each Diego> selector) And for more fun browse: 1 class allSelectors asSortedCollection and you'll see all messages that can be sent to "1". Repeat the same with: 1 class class allSelectors asSortedCollection and you'll see the same for 1's class (SmallInteger). -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 <[hidden email]> <URL:http://www.stonehenge.com/merlyn/> Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training! _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
Hi Diego and Randal,
Thanks for your examples. I printed them out, read them. I'll read them again tomorrow, try them in Workspace. This thread has been quite profitable for me. The Explorer has just had it's noise-to-signal-ratio reduced by an order of magnitude. Chris _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
Free forum by Nabble | Edit this page |