Warning: This is the manual of the legacy Guile 2.0 series. You may want to read the manual of the current stable series instead.

Next: , Previous: , Up: Utility Functions   [Contents][Index]


6.11.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.

Guile’s representation of an object property is a procedure-with-setter (see Procedures with Setters) that can be used with the generalized form of set! (REFFIXME) to set and retrieve that property for any Scheme object. So, setting a property looks like this:

(set! (my-property obj1) value-for-obj1)
(set! (my-property obj2) value-for-obj2)

And retrieving values of the same property looks like this:

(my-property obj1)
⇒
value-for-obj1

(my-property obj2)
⇒
value-for-obj2

To create an object property in the first place, use the make-object-property procedure:

(define my-property (make-object-property))
Scheme Procedure: make-object-property

Create and return an object property. An object property is a procedure-with-setter that can be called in two ways. (set! (property obj) val) sets obj’s property to val. (property obj) returns the current setting of obj’s property.

A single object property created by make-object-property can associate distinct property values with all Scheme values that are distinguishable by eq? (ruling out numeric values).

Internally, object properties are implemented using a weak key hash table. This means that, as long as a Scheme value with property values is protected from garbage collection, its property values are also protected. When the Scheme value is collected, its entry in the property table is removed and so the (ex-) property values are no longer protected by the table.

Guile also implements a more traditional Lispy interface to properties, in which each object has an list of key-value pairs associated with it. Properties in that list are keyed by symbols. This is a legacy interface; you should use weak hash tables or object properties instead.

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.


Next: , Previous: , Up: Utility Functions   [Contents][Index]