Next: , Previous: , Up: iklib unwind-protect   [Index]


6.9.7 Use with loop syntaxes

The unwind–protection mechanism has special integration with the loop syntaxes defined by (vicare); if an unwind–protection syntax is used in the body of a loop as defined by do, while, until, for and break or continue are used in the body forms: the cleanup forms are evaluated correctly. We have to remember that break and continue are implemented by escaping continuations.

This example shows breaking out of a while syntax; the body and the unwind handler are evaluated only once; exit is never called.

#!vicare
(import (vicare))
(define x 3)
(define y #f)
(while (positive? x)
  (with-unwind-protection
      (lambda (why)
        (set! y #t))
    (lambda ()
      (-- x)
      (break)
      (exit))))
x ⇒ 2
y ⇒ #t

This example shows using continue in a while syntax; the body and the unwind handler are evaluated 3 times; exit is never called.

#!vicare
(import (vicare))
(define x 3)
(define y 0)
(while (positive? x)
  (with-unwind-protection
      (lambda (why)
        (++ y))
    (lambda ()
      (-- x)
      (continue)
      (exit))))
x ⇒ 0
y ⇒ 3