[E-Lang] Internal vs. external iteration

Dean Tribble tribble@e-dean.com
Mon, 26 Mar 2001 00:32:20 -0800


Java use external iterators for collections (you create an 
Iterator/Enumerator and step through the elements).  E, Hydro, and JDBC use 
internal iteration (you pass to the collection's iteration routine an 
object that will be called with each element).  I'd like to have a brief 
discussion about this so that the design rationale is captured.

The concern that inspired this was a discussion some time ago about 
compiling loop termination to Java.  With internal iteration, the "break" 
construct actually needs to throw an exception.  Depending on the Java 
implementation, throwing an exception can be stunningly more expensive 
(*100-*1000) than returning or breaking.  Some of the forces are:

- breaking from a loop should not require the collection's cooperation
- the collection should be able to cleanup after loop termination
- each iteration should not allocate
- the iteration body should be able to force an exit from the enclosing method

I presume Tyler and MarkM can add things here, and talk about the presumed 
compilation strategy.


BTW A weird alternative is for iteartion cleanup is for the collection to 
E.send itself a message to cleanup from the iteration.  The loop is 
guaranteed to have completed before the message arrives.  This would allow 
external iteration with guaranteed eventual cleaup.