The following text is extracted from SRFI-35
(Conditions), from which STklos
conditions are derived.
Conditions are values that communicate information about exceptional
situations between parts of a program. Code that detects an exception
may be in a different part of the program than the code that handles
it. In fact, the former may have been written independently from the
latter. Consequently, to facilitate effective handling of exceptions,
conditions must communicate as much information as possible as
accurately as possible, and still allow effective handling by code
that did not precisely anticipate the nature of the exception that
occurred.
Conditions available in STklos are derived from SRFI-35 (Conditions) and in this
SRFI two mechanisms to enable this kind of communication are provided:
- subtyping among condition types allows handling code to
determine the general nature of an exception even though it does
not anticipate its exact nature,
- compound conditions allow an exceptional situation to be
described in multiple ways.
Conditions are structures with named slots. Each condition belongs to
one condition type (a condition type can be made from several
condition types). Each condition type specifies a set of
slot names. A condition belonging to a condition type includes a
value for each of the type's slot names. These values can be
extracted from the condition by using the appropriate slot name.
There is a tree of condition types with the distinguished &condition
as its root. All other condition types have a parent condition type.
Conditions are implemented with STklos structures (with a special bit
indicating that there are conditions). Of course, condition types are
implemented with structure types. As a consequence, functions on
structures or structures types are available on conditions or
conditions types (the contrary is not true). For instance, if C is a
condition, the expression
is a simple way to see it's slots and their associated value.
(make-condition-type id parent slot-names) | STklos procedure |
Make-condition-type returns a new condition type. Id must be a symbol
that serves as a symbolic name for the condition type. Parent must itself
be a condition type. Slot-names must be a list of symbols. It identifies
the slots of the conditions associated with the condition type.
|
(condition-type? obj) | STklos procedure |
Returns #t if obj is a condition type, and #f otherwise |
(make-compound-condition-type id ct1 ...) | STklos procedure |
Make-compound-condition-type returns a new condition type, built
from the condition types ct1 , ...
Id must be a symbol that serves as a symbolic name for the
condition type. The slots names of the new condition type is the
union of the slots of conditions ct1 ...
Note: This function is not defined in SRFI-34 (Exception Handling for Programs). |
(make-condition type slot-name value ...) | STklos procedure |
Make-condition creates a condition value belonging condition type
type . The following arguments must be, in turn, a slot name and an
arbitrary value. There must be such a pair for each slot of type and
its direct and indirect supertypes. Make-condition returns the
condition value, with the argument values associated with their
respective slots.
(let* ((ct (make-condition-type 'ct1 &condition '(a b)))
(c (make-condition ct 'b 2 'a 1)))
(struct->list c))
⇒ ((a . 1) (b . 2))
|
|
(condition? obj) | STklos procedure |
Returns #t if obj is a condition, and #f otherwise |
(condition-has-type? condition condition-type) | STklos procedure |
Condition-has-type? tests if condition belongs to condition-type .
It returns #t if any of condition 's types includes condition-type
either directly or as an ancestor and #f otherwise.
(let* ((ct1 (make-condition-type 'ct1 &condition '(a b)))
(ct2 (make-condition-type 'ct2 ct1 '(c)))
(ct3 (make-condition-type 'ct3 &condition '(x y z)))
(c (make-condition ct2 'a 1 'b 2 'c 3)))
(list (condition-has-type? c ct1)
(condition-has-type? c ct2)
(condition-has-type? c ct3)))
⇒ (#t #t #f)
|
|
(condition-ref condition slot-name) | STklos procedure |
Condition must be a condition, and slot-name a symbol. Moreover,
condition must belong to a condition type which has a slot name called
slot-name , or one of its (direct or indirect) supertypes must have the
slot. Condition-ref returns the value associated with slot-name .
(let* ((ct (make-condition-type 'ct1 &condition '(a b)))
(c (make-condition ct 'b 2 'a 1)))
(condition-ref c 'b))
⇒ 2
|
|
(condition-set! condition slot-name obj) | STklos procedure |
Condition must be a condition, and slot-name a symbol. Moreover,
condition must belong to a condition type which has a slot name called
slot-name , or one of its (direct or indirect) supertypes must have the
slot. Condition-set! change the value associated with slot-name to obj .
Note: Whereas condition-ref is defined in SRFI-35 (Conditions),
confition-set! is not. |
(make-compound-condition condition0 condition1 ...) | STklos procedure |
Make-compound-condition returns a compound condition belonging to
all condition types that the conditioni belong to.
Condition-ref , when applied to a compound condition will return
the value from the first of the conditioni that has such a slot. |
(extract-condition condition condition-type) | STklos procedure |
Condition must be a condition belonging to condition-type .
Extract-condition returns a condition of condition-type
with the slot values specified by condition . The new condition
is always allocated.
(let* ((ct1 (make-condition-type 'ct1 &condition '(a b)))
(ct2 (make-condition-type 'ct2 ct1 '(c)))
(c2 (make-condition ct2 'a 1 ' b 2 'c 3))
(c1 (extract-condition c2 ct1)))
(list (condition-has-type? c1 ct2)
(condition-has-type? c1 ct1)))
⇒ (#f #t)
|
|