next up previous

9.3.3.2 Default Value Facet

The default and default-dynamic facets can be used to specify an initial value given to a slot when an instance of the class is created or initialized. By default, a slot will have a default value which is derived from the slot's constraint facets (see sections 9.3.3.11 and 11.5). Default values are directly assigned to slots without the use of messages, unlike slot overrides in a make-instance call (see section 9.6.1).

The default facet is a static default: the specified expression is evaluated once when the class is defined, and the result is stored with the class. This result is assigned to the appropriate slot when a new instance is created. If the keyword ?DERIVE is used for the default value, then a default value is derived from the constraints for the slot (see section 11.5 for more details). By default, the default attribute for a slot is (default ?DERIVE). If the keyword ?NONE is used for the default value, then the slot is not assigned a default value. Using this keyword causes make-instance to require a slotoverride for that slot when an instance is created. Note that in CLIPS 6.0, a slot now has a default even if one is not explicitly specified (unlike CLIPS 5.1). This could cause different behavior for CLIPS 5.1 programs using the initialize-instance function. The ?NONE keyword can be used to recover the original behavior for classes.

The default-dynamic facet is a dynamic default: the specified expression is evaluated every time an instance is created, and the result is assigned to the appropriate slot.

Example

CLIPS> (clear)
CLIPS> (setgen 1)
1
CLIPS>
(defclass A (is-a USER)
    (role concrete)
    (slot foo (default-dynamic (gensym))
            (create-accessor read)))
CLIPS> (make-instance a1 of A)
[a1]
CLIPS> (make-instance a2 of A)
[a2]
CLIPS> (send [a1] get-foo)
gen1
CLIPS> (send [a2] get-foo)
gen2
CLIPS>


next up previous