[self-interest] Re: data structures

Jecel Assumpcao Jr jecel at lsi.usp.br
Wed Jun 9 06:48:09 UTC 1999

Ian Trudel wrote:
> : In Smalltalk we would do "vector new: 3" since everything is
> : initialized with nil, but Self doesn't like defaults and so you
> : have to choose some initial value (like 0 in this example)
> : explicitly.
> Out of interest, why Self doesn't like defaults? Time consuming? Personnaly,
> I prefer everything has a default value before uses. Maybe just because I
> am, and every programmer I know, are human and sometimes forgot to
> initialize.

Self is based on prototypes instead of classes. A class includes,
among many other things, a plan to build object instances of itself.
This plan (the #new method in Smalltalk, the constructor in other
languages) will normally replace any defaults that the system uses
when allocating the instance with values that are appropriate for
that class.

A prototype, on the other hand, is usually "hand made". It can be
cloned to generate new "instances", but it is an actual working
"instance" itself and its slot values should reflect this. We could
have a Record class in Smalltalk with this creation method:

   "creates an initialized Record object"
   ^ self basicNew initialize

where the instance method is:

   "sets the initial values for the Record object"
   name := 'unnamed'.
   age := 0.
   ^ self

We could do the exact same thing in Self, of course:

   (| parent* = traits clonable.
      copy = ("creates a new initialized record"
              resend.copy initialize).
      initialize = ("sets the initial values"
                    name: 'unnamed'.
                    age: 0.

but a the following would be much more Selfish:

   (| parent* = traits clonable.
      name <- 'unnamed'.
      age <- 0

In the first Self example, the slot values for the prototype are
the default (nil). They are a string and an integer for every
object clones from the prototype, however, so the prototype is not
a true representative of this kind of object. The second example
fixes this, but is now subject to the "prototype corruption
problem". That is when you change the prototype itself instead of
a copy (by sending it the message 'age: 10', for example). This
will effect every new clone created in the future.

So what I should have said in my comment what that in Self we should
always avoid depending on defaults like nil but should instead create
the prototypes with the right types of values (which are another kind
of defaults, of course).

-- Jecel


eGroups.com home: http://www.egroups.com/group/self-interest
http://www.egroups.com - Simplifying group communications

More information about the Self-interest mailing list