Using Arrays in Simkin


Simkin supports a special syntax to make accessing collections more convenient. For example:
var=array[1];
As with other features in Simkin, the interpreter is agnostic about the underlying object model of the application. The implementation of a collection is left to the C++ classes within the application.

When the interpreter encounters an array index operator [ ] in a script, it translates this into calls to the setValueAt and getValueAt methods exposed through the skExecutable base class.

For example the following results in a call to setValueAt:

array[1]="foo";
and this example results in a call to getValueAt:
var=array[1];

The script syntax allows for arbitrary expressions within the [ ], so for example the following are legal syntax:

var=array[1];
var=array["key"];
This means that the underlying C++ classes can expose collections which are keyed on any kind of value - so that ordinary Vectors, and Hashtables or Associative Arrays can all be supported.

Simkin is supplied with two classes which have array index support, skTreeNodeObject and skXMLElementObject. These both convert the array index to a numeric value before using it as an index into their collection. They also both assume the index is zero based. Both classes also support a field called numChildren which returns the number of elements in each collection.