next up previous
Next: Evaluating computed expressions Up: No Title Previous: Multiple return values

Unwind/wind protection

dynamic-wind, which was described previously in this column (when it was The Scheme Environment; Lisp Pointers, volume I, number 2), is already implemented in many Scheme dialects. dynamic-wind takes three arguments, all of which are thunks (procedures of no arguments). It behaves as if it were defined with =0pt=0pt=0pt =0pt[] (define (dynamic-wind before during after) (before) (call-with-values during (lambda results (after) (apply values results))))

except that the execution of the during thunk is ``protected'' against non-local entries and exits: a throw out of the execution of during will cause the after thunk to be invoked, and a throw from outside back in will cause the before thunk to be invoked. (By ``throw'' I mean an invocation of an explicit continuation as obtained from call-with-current-continuation.)

For details, the earlier Scheme Environment column refers the reader to Friedman and Haynes's paper ``Constraining Control'' in POPL 1985, but to save you the trouble of looking that up, I have supplied a more direct implementation of dynamic-wind in an appendix to the present column.

dynamic-wind was adopted with the following clarifications: The semantics of (dynamic-wind before during after) should leave unspecified what happens if a throw occurs out of before or after; and it is best to defer interrupts during before and after.



Margaret M Fleck
Tue Nov 5 21:19:46 CST 1996