View this PageEdit this PageUploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide

using dictionaries as inheritance structues

Home   How To   Code Pool   Public Library   Theory   Events

in order to reuse code that is repeatedly needed or, maybe even more, in order to
invent new rules of the game, new ways to allow and reject access and activity,
the most common thing is to write classes.

A class is a description of the way an object looks like. Objects represent an agent
that can be sent messages to, messages that either change internal state of the object
(instance variables) or do some calculation and return a value. In short, it means that an object consists of a number of functions (methods) and variables. Encapsulation in this context means that it is not important what the internal state of an object "really" is, because all access and change happens via messaging.


To make it more efficient to call these functions (to fine them amoungst a great number of them) they are compiled (cmd-K) into a method table. This means that methods that exist and the structure of a class cannot be changed without recompiling the whole class library.

one exception are unique methods that can be added to an object (.addUniqueMethod).

A more flexible way to organize access and allow encapsulation is now to use an IdentityDictionary that can equally contain functions and variables.
(
a = (
	x:4,
	y: { 1.0.rand }

);
a[\x] + a[\y].value;
)


If in an IdentityDictionary the intance variable 'know' is set to true, the dictionary acts like a class that understands messages. (Note that all messages that are implemented in class Object do not work in this way)

a.know_(true);

a.y
a.y


this provides a way to have an object like agent that has a behaviour which can be changed at runtime:

a[\y] = { Array.rand(8, 0, 1) }
a.y
//or even like this, using a 'setter'
a.y = { Array.rand(3, 0, 1) }
a.y


In order to be able to inherit behaviour from superclasses (superdictionaries), IdentityDictionary has an instance variable called "parent" and one called "proto". A key that is not found in the dictionary is looked up in the parent.

b = (z: pi);
a.parent = b;

a.y + a.z


Keys are searched in the 'proto' dictionary first and then in the 'parent', so it is possible to make a structure with multiple inheritance:

r = (fox:  1987);
r.know = true;
a.proto = r;
a.fox;

a.fox = 2006;








Link to this Page