[Newbies] Disussion on creating a new class

Ralph Johnson johnson at cs.uiuc.edu
Sun May 21 07:59:09 UTC 2006

On 5/20/06, cdrick <cdrick65 at gmail.com> wrote:
> I'd like to discuss on the way of creating a new class in smalltalk in
> general...

> So, here is the template to create a class when having a browser
> opened on a selected category:
> Object subclass: #NameOfSubclass
>     instanceVariableNames: ''
>     classVariableNames: ''
>     poolDictionaries: ''
>     category: 'Kernel-Objects'
> so, the class Object (more precisely the class class) receives the message :
> Class>>subclass:instanceVariableNames:classVariableNams:poolDictionaries:category:
> and uses a ClassBuilder that will return the newly created class.

Don't think of this as a message send.  Yes, this is legal Smalltalk
code, and it is a bit of an amusing trick that Squeak uses Smalltalk
code to declare a class.  But it is better for new programmers to just
think of this as a class definition.  You should definitely NOT think
of it as making a ClassBuilder; that is part of the current
implementation but has not always been part of the implementation, and
is certainly not part of the definition of class creation.

Yes, it is a little odd that the superclass comes first and that the
class name is a symbol.  As you said, the reasons are all due to the
trick of implementing this as a message send.  But it is easy define
classes because you don't have to remember this syntax, all you ever
have to do is to edit existing class definitions and put in new names.
 Like I said, it is better not to think about how this is implemented,
and to just copy existing class definitions.

If the syntax bothers you so much, define a new syntax for class
definitions and do NOT make it legal Smalltalk code.  Just change the
browser to display this syntax and to accept it.  For example, it
could be

class: 'NameOfSubclass'
superclass: 'Object'
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Kernel-Objects'

You could implement this by making it be a class method for Compiler,
or Class, or Browser, or even an instance method of Browser.  If you
leave fileouts alone, you can change the way class definitions are
displayed in the browser and not bother anybody else.  You can enter
class definitions using your favorite syntax ,and nobody else will
care.  That is because you would be making a new presentation for
class definitions but not changing the objects that you are editing.

-Ralph Johnson

More information about the Beginners mailing list