Next: , Previous: , Up: iklib syntaxes   [Index]


6.8.10 Capturing the current dynamic environment

Syntax: with-current-dynamic-environment ?exception-retvals-maker ?thunk

Install and return a thunk that, wherever it is called, evaluates ?thunk in the dynamic environment of the macro use and returns its return values. If ?thunk raises an exception: apply the procedure ?exception-retvals-maker to the raised object and return its return values.

Exceptions raised by ?exception-retvals-maker are not blocked.

Usage examples:

(import (vicare)
  (only (vicare checks)
        with-result
        add-result))

(define parm
  (make-parameter #f))

(with-result
  (parametrise ((parm 'outer))
    (let* ((counter 0)
           (thunk   (parametrise ((parm 'inner))
                      (with-current-dynamic-environment
                          values
                        (lambda ()
                          (set! counter (+ 1 counter))
                          (add-result (list 'inside-thunk (parm))))))))
      (add-result (parm))
      (add-result 'calling-thunk-1)
      (thunk)
      (add-result 'calling-thunk-2)
      (thunk)
      counter)))
⇒ (2 (outer
       calling-thunk-1 (inside-thunk inner)
       calling-thunk-2 (inside-thunk inner)))

(with-result
  (parametrise ((parm 'outer))
    (let* ((counter 0)
           (thunk   (parametrise ((parm 'inner))
                      (with-current-dynamic-environment
                          values
                        (lambda ()
                          (set! counter (+ 1 counter))
                          (add-result (list 'inside-thunk (parm)))
                          (add-result 'raise-exception)
                          (raise 123))))))
      (add-result (parm))
      (add-result 'calling-thunk-1)
      (thunk)
      (add-result 'calling-thunk-2)
      (thunk)
      counter)))
⇒ (2 (outer
       calling-thunk-1 (inside-thunk inner) raise-exception
       calling-thunk-2 (inside-thunk inner) raise-exception))