I'm REALLY new to Smalltalk. In a previous thread, I created a file
Hello.st: Object subclass: #Hello instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: ''! "Create a method" !Hello methodsFor: 'speaking'! publish Transcript show: 'Greetings. '! ! ! To load it, I do FileStream fileIn: 'Hello.st' ! BUT, if I want to "run" Hello, I have to do Hello new publish My question is: why did I have to `new` Hello, whereas I don't have to `new` FileStream? Presumably for Hello I have to create an object by instantiating a class, whereas with a class like FileStream I don't have to. What's going on? Are there methods which only belong to classes and not objects, and if so, how do I define them? Sorry if the question seems really dumb. _______________________________________________ help-smalltalk mailing list [hidden email] http://lists.gnu.org/mailman/listinfo/help-smalltalk |
Mark Carter <alt.mcarter <at> googlemail.com> writes:
> > I'm REALLY new to Smalltalk. In a previous thread, I created a file > Hello.st: > > Object subclass: #Hello > instanceVariableNames: '' > classVariableNames: '' > poolDictionaries: '' > category: ''! > > "Create a method" > !Hello methodsFor: 'speaking'! > publish > Transcript show: 'Greetings. '! > > ! ! > > To load it, I do > FileStream fileIn: 'Hello.st' ! > > BUT, if I want to "run" Hello, I have to do > Hello new publish > > My question is: why did I have to `new` Hello, whereas I don't have to > `new` FileStream? Presumably for Hello I have to create an object by > instantiating a class, whereas with a class like FileStream I don't > have to. What's going on? Are there methods which only belong to > classes and not objects, and if so, how do I define them? > > Sorry if the question seems really dumb. > Yes you guessed right, there are class methods. You can create them like this: !Hello class methodsFor: 'speaking'! Notice that I changed "Hello" into "Hello class". I am also a new Smalltalker so maybe I'm wrong but as far as I know "class" is a message which returns the class of an object. So the code below is the new hello.st and works with both "Hello new publish" or just "Hello publish". Object subclass: #Hello instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: ''! "Create a class method" !Hello class methodsFor: 'speaking'! publish Transcript show: 'Greetings. '! ! ! "Create a method" !Hello methodsFor: 'speaking'! publish Transcript show: 'Greetings. '! ! ! Hello publish! _______________________________________________ help-smalltalk mailing list [hidden email] http://lists.gnu.org/mailman/listinfo/help-smalltalk
Canol Gökel
|
In reply to this post by Mark Carter-4
On Tue, 27 May 2008 21:24:44 +0100
Mark Carter <[hidden email]> wrote: > To load it, I do > FileStream fileIn: 'Hello.st' ! > > BUT, if I want to "run" Hello, I have to do > Hello new publish > > My question is: why did I have to `new` Hello, whereas I don't have to > `new` FileStream? Presumably for Hello I have to create an object by > instantiating a class, whereas with a class like FileStream I don't > have to. What's going on? In Smalltalk, everything is an object. You make objects do something by sending them a message for which they (or their ancestors) have an implementing method. "Hello new publish" asks the class object referenced by Hello to create a new instance, which is then asked to publish something. That's the "normal" way of doing things. But you can define class side methods (often as convenient abbreviations) for creating new instances, which "hide" the call to new within their implementation, your fileIn above is one such example. It's implementation might very well look like: fileIn: aPath ^self new fileIn: aPath Just guessing, but it's a common pattern. An interesting aspect of "everything is an object" is the following: If everything is an object, even classes, then where does the class object Hello come from? You made it by asking the Object class object to create a subclass: Object subclass: #Hello instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: ''! This "class definition" is not weird syntax, it's "just another message", sent to Object when you accepted/evaluated the class definition message. s. _______________________________________________ help-smalltalk mailing list [hidden email] http://lists.gnu.org/mailman/listinfo/help-smalltalk |
> But you can define class side methods (often as convenient > abbreviations) for creating new instances, which "hide" the call to new > within their implementation, your fileIn above is one such example. > It's implementation might very well look like: > > fileIn: aPath > ^self new fileIn: aPath > > Just guessing, but it's a common pattern. Yes, in this case it's another common usage of class methods, i.e. "create an object, do something, discard it". fileIn: is implemented as ^(self open: aPath mode: self read) fileIn; close (more or less). Paolo _______________________________________________ help-smalltalk mailing list [hidden email] http://lists.gnu.org/mailman/listinfo/help-smalltalk |
Free forum by Nabble | Edit this page |