Next: Sorting, Previous: Equality, Up: Utility Functions
It's often useful to associate a piece of additional information with a Scheme object even though that object does not have a dedicated slot available in which the additional information could be stored. Object properties allow you to do just that.
An object property is most commonly used to associate one kind of
additional information with each instance of a class of similar Scheme
objects. For example, all procedures have a `name' property, which
stores the name of the variable in which the procedure was stored by a
define expression, or #f if the procedure wasn't created
by that kind of expression.
Guile provides two ways for managing object properties: using a getter/setter procedure, and using property lists.
Return a procedure-with-setter prop that can be used to get/set a single property of arbitrary objects. Optional arg default specifies the default value for objects that do not yet have this property. If unspecified, use #f. For example:
(define otw (make-object-property 99)) ;; on the wall (otw 'beers) => 99 (set! (otw 'beers) (1- (otw 'beers))) => 98Note that
(set! (P obj) val)returnsval.A single object property created by
make-object-propertycan associate distinct property values with all Scheme values that are distinguishable byeq?(including, for example, integers).Implementation Note: Setting an object's property to default is guaranteed to release storage associated with the relation.
Traditionally, Lisp systems provide a different object property
interface to that provided by make-object-property, in which the
object property that is being set or retrieved is indicated by a symbol.
Guile supports this approach, extending it to allow any object (not just
symbols) to be the property.
Return obj's property list.
Set obj's property list to alist.