Supporting the for each statement


Simkin supports a special syntax to make iterating over collections more convenient. For example:
for each item in collection {
	  trace(item.name);
}
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 Java or C++ classes within the application.

When the interpreter encounters the for each statement, it calls the createIterator method on the underlying skExecutable class (C++) or Executable interface (Java).

This function should return a new class which is an instance of a class derived from the skExecutableIterator class (C++), or implementing the ExecutableIterator interface (Java).

The for each statement has two forms:

  1. unqualified - this means the scripter wants to iterate over all members of the collection, e.g.:
    for each person in group {
    	  trace(person.name);
    }
    
  2. qualified - the scripter wants to apply a filter to the iteration. This is specified as an identifier, which is passed to the createIterator function. You can interpreter this in any way you choose. e.g.:
    for each Manager employee in company {
    	  trace(employee.name);
    }
    
In the C++ version of Simkin, once the createIterator method returns an iterator object, the Interpreter repeatedly calls the next method on the iterator until false is returned.

When the iteration is completed, the Interpreter will use delete to destroy the iterator.