Mixed feelings

Boris_Gaertner at msg.de Boris_Gaertner at msg.de
Thu Jun 6 08:34:09 UTC 2002


Skipped content of type multipart/alternative-------------- next part --------------
A non-text attachment was scrubbed...
Name: UnivariatePolynomial.st
Type: application/octet-stream
Size: 9073 bytes
Desc: not available
Url : http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20020606/e52db8b5/UnivariatePolynomial.obj
-------------- next part --------------
Variable subclasses - A very short Tutorial
-------------------------------------------

It is widely believed (and essentially correct) that
classes are the brickstones of Smalltalk. It is however
not always mentioned that there are two different types
of classes: Those that are used to create instances of
uniform size and those that are used to create instances
of varying size.

Here is the definition of a class that is used to
create instances of uniform size:

Object subclass: #Point
	instanceVariableNames: 'x y '
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Graphics-Primitives'

The instance  Point x: 10 y: 20  has the same size
as every other instance of Point. Every instance has two
named instance variables: x and y. 

And here is the definition of a class that is used to
create instances of varying size: 

ArrayedCollection variableSubclass: #Array
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Collections-Arrayed'

Please note that this subclass is created with
the message
   variableSubclass:instanceVariableNames:classVariableNames:poolDictionaries:category: 

The instance  (a1 := Array new: 10)  has ten indexable
instance variables. The instance (a2 := Array new: 20)
has twenty indexable instance variables. Both instances
differ in size. To prove this, you evaluate the
expression:  a1 size ~= a2 size.  This expression
evaluates to  true.
The instance variables of an array are not named,
but indexed. To access indexed instance variables, you
have to use the messages  #at:  and  #at:put:

for example:

    a1 at: 3 put: 'hallo'.
    a1 at: 3.

Here is yet another definition of a class that is
used to create instances of varying size:

ArrayedCollection variableByteSubclass: #String
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Collections-Text'

A string is an array of characters, so a string is
a specialized array. The differences are:

1. The indexed variables of a string have byte size.
2. The indexed variables of a string can only store
   instances of Character.

Here is yet another kind of a variable class:

ArrayedCollection variableWordSubclass: #FloatArray
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Collections-Arrayed'

Instances of this class vary in size. The indexed
variables have word size. (The size of a word is
32 bits in Squeak). 


===================================================
General facts:
--------------

All instances of ordinary subclasses have the same size.
Instances of variable subclasses can differ in size.


A variable subclass can be derived from either an ordinary or
a variable subclass. In any case, the declaration looks like:

<superclass>  variableSubclass: #<classname>
	instanceVariableNames: ''
	classVariableNames: ''
	poolDictionaries: ''
	category: <name of category>

An instance of a variable subclass is created with
the class method  #new:  The argument is required to be
a nonnegative integer. The integer value defines the number of
indexable variables of the instance.

Examples: 
Array new: 13.
String new: 0.

Hint: The method #new: is defined in the *instance*
protocol of class Behavior.

Access to the indexable variables:
Indexable instance variables do not have names. They can
only be accessed with the methods #at: #at:put: and
with #basicAt:  #basicAt:put:

Besides indexable variables, a variable subclass can
have named instance variables. The named instance variables
are enumerated in the class creation statement.


Variable size classes are infrequently defined.
To find all variable subclasses in your image,
execute this with "inspect":

Object allSubclasses select:
   [:class | class isVariable]

The result is a set. Compare its size with the
number of all subclasses of Object, which you
compute with Object allSubclasses size.
You will find that only about 2 percent of all
classes are variable size classes. Looking a
bit closer, you will find out that most variable
classes are used to interface to the virtual
machine or to some plugins.

Variable byte subclasses
A variable byte subclass is a specialized form
of a variable subclass. Its indexable data items
are bytes.  The justification for these classes
is the need for an efficient representation of
strings and byte arrays. It is therefore not a
surprise that  String  and  ByteArray (and also
CompiledMethod)  are variable byte subclasses.

Variable word subclasses
A variable word subclass is a specialized form
of a variable subclass. Its indexable data items
are 32-bit words. Examples are
FloatArray B3DFloatArray B3DColor4 B3DIndexedQuadArray
Instances of these classes are used as working
memory by some specialized funtions of the VM.

Restrictions:
A variable subclass cannot be the superclass of
an ordinary class, but it can be the superclass
of another variable subclass.

In some Smalltalks, a variable byte subclass is
not allowed to have named instance variables,
in Squeak it is. I can not tell you about
ANSI Smalltalk, can anybody else?


More information about the Squeak-dev mailing list