[self-interest] Re: data structures

Jecel Assumpcao Jr jecel at lsi.usp.br
Tue Jun 8 19:07:48 UTC 1999


Jose' B. Garcia Perez-Schofield wrote:
>         (|parent* = traits oddball. contents *= vector |)
> 
>         And that's the problem: I simply can't do this because I don't
> know how to do it.  :-(
>         I couldn't find information about it into the Self 4.0
> Programmer's Reference (probably it is my fault).

Actually, the solution isn't at all obvious unless you really
think about it:

     (| parent* = traits oddball.
        contents = vector copySize: 3 FillingWith: 0
     |)

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.

>         The object below doesn't work: if I send a message copyAddLast,
> then I get an error. And I don't know how to reference the objects,
> because they have no name ('an object').

I didn't understand to which object you sent the 'copyAddLast:'
message. I don't remember if vectors understand this message, but
it seems reasonable that they would. About the name problem, you
could try:

  (| parent* = traits oddball.
     contents = vector copySize: 3 FillingWith: 0.
     record = (| parent* = (| parent* = traits clonable.
                              getAge = (age).
                              setAge: x = (Age: x).
                              getName: (name).
                              setName: x = (name: x) | ).
                 age <-0.
                 name <- 0. | )
  |)

You can now try the following in the evaluator for this most
external object:

  contents at: 0 Put: record copy.
  contents at: 1 Put: record copy.
  contents at: 2 Put: record copy.
  (contents at: 0) setAge: 25.
  (contents at: 0) setName: 'Charles Babbage'.
  (contents at: 1) setAge: 2.
  (contents at: 1) setName: 'Lisa'.

You get the idea. You can also build the same object a piece at
a time graphically, but it would be a bit hard to describe
textually.

>         Another possibilitie is:
> 
>         (|parent* = traits oddball. contents <- (0 & 0 & 0). | )
> 
>         And, later, change the zeroes with my objects, but I don't know how.

You must convert the 'collector' object created by the '&' message
into some collection, such as a vector or a set:

         contents <- (0 & 0 & 0) asVector.

For your database, a vector probably wouldn't be the best option
as you might want to add and delete elements. Self includes lots
of data structures, such as sets, lists, sorted trees and so on.
Please note that this could be an interesting was to initialize
your contents slot:

     contents <- (record copy & record copy & record copy) asVector.

With prototype based programming, it is better if a slot's value
reflect its intended use at all times, instead of being initialized
to one thing and then changed to another later.

I hope that helps,
-- 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