I have been thinking about adding a while loop construct to the template language. This would permit looping based on a count or some test between two lists. The construct would be what one would expect:
Code:
while expr do
<<stuff>>
od
An alternate form could be
Code:
while expr:
<<stuff>>
od
I prefer the first because the correspondence between "do" and "od" is clearer.
The problem: this construct would for the first time permit infinite loops in the template language. A template infinite loop would freeze the GUI and be nearly impossible to fix because the user could never get past the loop to the editor.
One possible fix is to limit the number of iterations to something, perhaps 1000. The loop would exit if this limit is hit. That would permit getting to the editor to fix it, albeit with some delays. This solution opens the door to a language construct to specify the limit, for example:
Code:
while expr limit <numeric expression> do
<<stuff>>
od
Another solution would be to set a computation time limit, perhaps 1 second, but this would be slower because it would require checking the elapsed time every iteration (or every N interations where N is small). Of course the time limit could be set as above, only with the limit in milliseconds instead of iterations.
An alternate construct would be to avoid the problem by not using a while loop but instead require a counting for loop and a break statement, such as:
Code:
for var range N to M:
<<stuff>>
if <something> then break fi;
<<stuff>>
rof
This construct would loop max(0, (M - N)) times. I would still need either to put a maximum on (M - N) or use a maximum duration.
Thoughts on
- whether a while loop is useful (use cases are welcome)
- which form you prefer
- how to deal with infinite loops
will be appreciated.