[comp.lang.ada] Iterate constructs

WILSON@AMSTEL.llnl.gov (One Heppy Heppy 'Ket') (04/11/90)

Erland Sommarskog considers the virtues of an out mode "Continue" parameter
for generic iteration routines vs. using an exception to exit that routine.

My preference would be to using the "Continue" parameter, even though it
might not be used more than 10% of the time.  There are several issues here.

First, using the parameter makes it obvious just what functionality is
provided by the routine, at the specification level.  Second, it's important
that the iterate routine know that a user exception may be raised, in case
the iterate routine needs to clean up the data structure before exiting;  if
this mechanism is to be used, this cleanup needs to be performed in an internal
"when others" exception handler, which can accidentally mask internal errors.
Third, there is the philosophical problem of raising an exception in a
non-exceptional case.  Normally I leave this issue up to the individual
programmers, but I don't like the idea of writing a general purpose package
which requires that behavior of the package user.

There are two other alternatives.  First, the generic package itself could
define the exception DONE, and require the user to raise that exception to
get out early.  This has the advantage that it is defined in the package
specification, and that the package body can trap for that exception
explicitely, and that the instantiated procedure does not need to exit
itself with an exception.

The other alternative is to define two iterate routines, ITERATE and
ITERATE_ALL, where ITERATE takes a generic procedure with the "Continue"
parameter.  This has the disadvantage of cluttering up the specification with
a second routine with little added value, but it avoids the exception issue
altogether.

My preference is for the single ITERATE with a "Continue" parameter.  We use
this construct in many of our packages, and I'd guess that we take advantage
of the early exit around 1/2 the time.  (One problem we have with this is that
if the user forgets to provide a value of "TRUE" for this parameter, our
compiler does not mention that we have an unassigned out mode parameter, and
the iterate routine exits unpredictably.)

			--- Rick Wilson