Next: , Previous: Condition Instances, Up: Condition Instances


16.5.1 Generating Operations on Conditions

Scheme provides four procedures that take a condition type as input and produce operations on the corresponding condition object. These are reminiscent of the operations on record types that produce record operators (see Records). Given a condition type it is possible to generate: a constructor for instances of the type (using condition-constructor); an accessor to extract the contents of a field in instances of the type (using condition-accessor); a predicate to test for instances of the type (using condition-predicate); and a procedure to create and signal an instance of the type (using condition-signaller).

Notice that the creation of a condition object is distinct from signalling an occurrence of the condition. Condition objects are first-class; they may be created and never signalled, or they may be signalled more than once. Further notice that there are no procedures for modifying conditions; once created, a condition cannot be altered.

— procedure: condition-constructor condition-type field-names

Returns a constructor procedure that takes as arguments values for the fields specified in field-names and creates a condition of type condition-type. Field-names must be a list of symbols that is a subset of the field-names in condition-type. The constructor procedure returned by condition-constructor has signature

          (lambda (continuation restarts . field-values) ...)
     

where the field-names correspond to the field-values. The constructor argument restarts is described in Restarts. Conditions created by the constructor procedure have #f for the values of all fields other than those specified by field-names.

For example, the following procedure make-simple-warning constructs a condition of type condition-type:simple-warning given a continuation (where the condition occurred), a description of the restarts to be made available, a warning message, and a list of irritants that caused the warning:

          (define make-simple-warning
            (condition-constructor condition-type:simple-warning
                                   '(message irritants)))
     
— procedure: condition-accessor condition-type field-name

Returns a procedure that takes as input a condition object of type condition-type and extracts the contents of the specified field-name. condition-accessor signals error:bad-range-argument if the field-name isn't one of the named fields of condition-type; the returned procedure will signal error:wrong-type-argument if passed an object other than a condition of type condition-type or one of its specializations.

If it is known in advance that a particular field of a condition will be accessed repeatedly it is worth constructing an accessor for the field using condition-accessor rather than using the (possibly more convenient, but slower) access-condition procedure.

— procedure: condition-predicate condition-type

Returns a predicate procedure for testing whether an object is a condition of type condition-type or one of its specializations (there is no predefined way to test for a condition of a given type but not a specialization of that type).

— procedure: condition-signaller condition-type field-names default-handler

Returns a signalling procedure with parameters field-names. When the signalling procedure is called it creates and signals a condition of type condition-type. If the condition isn't handled (i.e. if no handler is invoked that causes an escape from the current continuation) the signalling procedure reduces to a call to default-handler with the condition as its argument.

There are several standard procedures that are conventionally used for default-handler. If condition-type is a specialization of condition-type:error, default-handler should be the procedure
standard-error-handler. If condition-type is a specialization of condition-type:warning, default-handler should be the procedure standard-warning-handler. If condition-type is a specialization of condition-type:breakpoint, default-handler should be the procedure standard-breakpoint-handler.