Using Arrays in Simkin


Simkin supports a special syntax to make accessing collections more convenient. For example:
var=array[1];
Simkin for Java supports the array index feature in one of two ways:
  1. If the underlying object is a native Java array, the interpreter will automatically retrieve or set the value in the array. The Interpreter also supports the length or numChildren attribute of a native array, which will return the size of the array
  2. If the underlying object implements the Executable interface, the Interpreter will call the setValueAt or getValueAt methods

For example, if array implements the Executable interface, the 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"];
If the underlying array is a native Java array, the interpreter will always convert the expression to a number, which is zero-based.

If the underlying object implements the Executable interface, the value is passed in "as-is" to setValueAt or getValueAt. This means that the underlying Java 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, TreeNodeObject and XMLElementObject. 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.