Next: , Previous: Equality, Up: Utility Functions


24.2 Object Properties

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.

— Scheme Procedure: make-object-property [default]

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))) => 98

Note that (set! (P obj) val) returns val.

A single object property created by make-object-property can associate distinct property values with all Scheme values that are distinguishable by eq? (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.

— Scheme Procedure: object-properties obj
— C Function: scm_object_properties (obj)

Return obj's property list.

— Scheme Procedure: set-object-properties! obj alist
— C Function: scm_set_object_properties_x (obj, alist)

Set obj's property list to alist.

— Scheme Procedure: object-property obj key
— C Function: scm_object_property (obj, key)

Return the property of obj with name key.

— Scheme Procedure: set-object-property! obj key value
— C Function: scm_set_object_property_x (obj, key, value)

In obj's property list, set the property named key to value.