hi there,<br><br>I have a little helper function that does something like this:<br><br>(define (helper thunk)<br> (setup)<br> (let ((result (thunk))<br> (teardown)<br> result))<br><br>Meaning my helper wraps a call to a thunk with some setup/teardown actions and returns whatever thunk returned. If thunk raises an error however, I do not want for teardown to be called.
<br><br>Assuming I have no control over thunk, how do I make the helper cope with possible multiple return values from thunk?<br><br>Here is my attempt:<br><br>(define (helper thunk)<br> (let ((success #t))<br> (dynamic-wind
<br> (lambda () (printf "Setup~n"))<br> (lambda ()<br> (with-handlers (((lambda ex #t)<br> (lambda (exn)<br> (set! success #f)<br> (raise exn))))
<br> (thunk)))<br> (lambda()<br> (if success<br> (printf "Teardown~n"))))))<br><br>> (helper (lambda () (values 1 2 3)))<br>Setup<br>Teardown<br>1<br>2<br>3<br><br>> (helper (lambda () 1))a
<br>Setup<br>Teardown<br>1<br><br>> (helper (lambda () (error "Abort") (values 1 2 3)))<br>Setup<br>> Abort<br><br>This seems to work, but its ugly. Is there a nicer solution?<br><br>-pp<br><br><br><br><br>
-pp<br><br>