Next: , Previous: Tracing and (ice-9 debug), Up: Traps Traps Installing More Traps

Sometimes it is desirable for the behaviour at one trap to install further traps. In other words, the behaviour is something like “Don't do much right now, but set things up to stop after two or three more steps”, or “... when this frame completes”. This is absolutely fine. For example, it is easy to code a generic “do so-and-so when the current frame exits” procedure, which can be used wherever a trap context is available, as follows.

     (define (at-exit trap-context behaviour)
       (install-trap (make <exit-trap>
     		  #:depth (tc:depth trap-context)
     		  #:single-shot #t
     		  #:behaviour behaviour)))

To continue and pin down the example, this could then be used as part of a behaviour whose purpose was to measure the accumulated time spent in and below a specified procedure.

     (define calls 0)
     (define total 0)
     (define accumulate-time
       (lambda (trap-context)
         (set! calls (+ calls 1))
         (let ((entry (current-time)))
           (at-exit trap-context
             (lambda (ignored)
               (set! total
                     (+ total (- (current-time)
     (install-trap (make <procedure-trap>
                     #:procedure my-proc
                     #:behaviour accumulate-time))