[Newbies] Disussion on creating a new class

cdrick cdrick65 at gmail.com
Sat May 20 21:10:41 UTC 2006

Hi All

I'd like to discuss on the way of creating a new class in smalltalk in
general... I hope I'll be clear enough... :) When I first start, I
found the way of creating classes not that straightforward... Probably
because this implies a minimum understanding of smalltalk...

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 :
and uses a ClassBuilder that will return the newly created class.

Some things bothers me:

*******  if we want to create a class in a selected category, we
change #NameOfSubclass in the symbol #NewNameClass
using a symbol is not obvious (for me)... I'd prefer something like:

                subclassOf: aClass ; --> (Superclass)
                instanceVariableNames: '' ;
                classVariableNames: '' ;
                poolDictionaries: '' ;
                category: 'KernelTests-Methods' ;

But NewClass is not (yet) an object, and it's interesting to see how
smalltalk consider it.. actually this is nothing, and smalltalk detect
it while parsing the evaluated text selection ... it is varName (a
string from the selection) in the following message to Parser:

Parser>> variable
   | varName varStart varEnd |
   varStart _ self startOfNextToken + requestorOffset.
   ***varName _ self advance.***
   varEnd _ self endOfLastToken + requestorOffset.
   ^ encoder encodeVariable: varName
         sourceRange: (varStart to: varEnd)
         ifUnknown: [self correctVariable: varName interval: (varStart
to: varEnd)]

What I'd like is to be able to create from an UnknowObject (that could
be a subclass of UndefinedObject ? would it be problematic,
interesting ?)

nb: There is actually a way to create a class from the execution of an
unknown text selection. Excuting   NewTerm   (do-it)  opens a menu
that gives the possibility to create a new class (this only works if
the selection start with an uppercase and it's done before excuting by
the parser).

******** Also, if we want to subclass a given class (which is
frequent) it's not direct as we need to change in the template the
receiver into the given class and the first argument of
#subclass:instanceVariableNames:.... in the symbol #NewNameClass.

Yet, It exists a more straight way to do that by right clicking on a
selected class in the browser and then choosing 'new class'. And so it
changes the template and wait for us to accept once proper parameters
are entered...

nb: I think 'new subclass' should be more appropriate? - new class
should only allow to create a subclass of object.

******** Last, I think that when created, the symbol with the name
should be instead directly the class...Not so easy to figure out from
what we see... Something like:

SequenceableCollection subclass: OrderedCollection --> (the class, not
the symbol)
       instanceVariableNames: 'array firstIndex lastIndex'
       classVariableNames: ''
       poolDictionaries: ''
       category: 'Collections-Sequenceable'

OrderedCollection subclassOf: SequenceableCollection  ...

An interesting (long) method for class creation is:

So I hope you understood what I've tried to say :)

If you have any comments or remarks concerning all that, I'll
appreciate having others opinions.

To sum up:
- using an UnknownObject (and especially UnknownObject>>subclassOf: aClass)
- replacing symbols with appropriate classes when possible (when
viewing a class definition)...

Thanks and see you ;)


More information about the Beginners mailing list