[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-smalltalk] Objects and classes

From: Stefan Schmiedl
Subject: Re: [Help-smalltalk] Objects and classes
Date: Wed, 28 May 2008 07:48:59 +0200

On Tue, 27 May 2008 21:24:44 +0100
Mark Carter <address@hidden> wrote:

> To load it, I do
> FileStream fileIn: '' !
> 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.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]