[Date Prev][Date Next][Thread Prev][Thread Next][Author Index][Date Index][Thread Index]

A Macro for Steppers



Abstract:  It's easy to forget to put the Stepper::step() call in loops using
Steppers.  I propose (after discussion with HughH) a macro for stepper loops
and a corresponding stepper method in Smalltalk.  There is an outstanding
problem, however, with returning from one of these in Smalltalk.

I've spent several hours chasing down infinite loop bugs that resulted from
the omission of a step() call in loops using Steppers.  I'm apparently not
alone in this.  It seems to me that most stepper loops are of one form that
could be generated by an X++ macro or interpreted by a Smalltalk method.
The most common form of stepper loop is
	while ( stepper->hasValue() ) {
	  ... some code ...
	  stepper->step();
	}

To make sure that the step() call alway appears, I've thought of a macro
	#define STEP_WITH(STEPPER_NAME, BLOCK)			\
		while (STEPPER_NAME->hasValue()) {		\
		   BLOCK ;					\
		   STEPPER_NAME->step();			\
		}

which would be used as

	STEP_WITH( aStepper,
		   {
			if ( wereLookingFor (aStepper->key()) ) {
			  doSomethingWith (aStepper->get());
			}
		   } );

for example.  By the way, suggestions are welcome to replace the name
STEP_WITH.

In Smalltalk, the method

	stepWith: aBlock

could be added to Steppers, and the translator could turn it into a call to
the above X++ macro.

Note that both of these retain the Stepper virtue of keeping the actions
on the Stepper inline, as opposed to being executed by some far off
UnaryFn-like method.

A problem arises when the action in the Stepper loop involves a return
from the embodying function.  The execution scopes of X++ and Smalltalk
differ somewhat here:  A return in the X++ macro will return from the
function that uses the macro, whereas a return in the block in Smalltalk
would just return from the block, but not the calling method.  HughH is
suspicious that a solution exists for this problem, but we don't have it
yet. (Dean?)

The X++ version will appear when I decide where it should be defined.

	- e -