Next: , Up: Traps


5.21.3.2 How to Set a Trap

Setting a trap is done in two parts. First the trap is defined by creating an instance of the appropriate trap class, with slot values specifying the condition under which the trap will fire and the action to take when it fires. Secondly the trap object thus created must be installed.

To make this immediately concrete, here is an example that sets a trap to fire on the next application of the facti procedure, and to handle the trap by entering the command line debugger.

     (install-trap (make <procedure-trap>
                     #:procedure facti
                     #:single-shot #t
                     #:behaviour debug-trap))

Briefly, the elements of this incantation are as follows. (All of these are described more fully in the following subsubsections.)

It is of course possible for the user to define more convenient shorthands for setting common kinds of traps. See Trap Shorthands, for some examples.

The ability to install, uninstall and reinstall a trap without losing its definition is Guile's equivalent of the disable/enable commands provided by debuggers like GDB.

— Generic Function: install-trap trap

Install the trap object trap, so that its behaviour will be executed when the conditions for the trap firing are met.

— Generic Function: uninstall-trap trap

Uninstall the trap object trap, so that its behaviour will not be executed even if the conditions for the trap firing are met.