Next: , Previous: Making New Objects, Up: Top

5 Accessing Slots

There are several ways to access slot values in an object. The naming and argument-order conventions are similar to those used for referencing vectors (see Vectors).

— Macro: oset object slot value

This macro sets the value behind slot to value in object. It returns value.

— Macro: oset-default class slot value

This macro sets the :initform for slot in class to value.

This allows the user to set both public and private defaults after the class has been constructed, and provides a way to configure the default behavior of packages built with classes (the same way setq-default does for buffer-local variables).

For example, if a user wanted all data-objects (see Building Classes) to inform a special object of his own devising when they changed, this can be arranged by simply executing this bit of code:

          (oset-default data-object reference (list my-special-object))
— Macro: oref obj slot

Retrieve the value stored in obj in the slot named by slot. Slot is the name of the slot when created by defclass or the label created by the :initarg tag.

— Macro: oref-default obj slot

Gets the default value of obj (maybe a class) for slot. The default value is the value installed in a class with the :initform tag. slot can be the slot name, or the tag specified by the :initarg tag in the defclass call.

The following accessors are defined by CLOS to reference or modify slot values, and use the previously mentioned set/ref routines.

— Function: slot-value object slot

This function retrieves the value of slot from object. Unlike oref, the symbol for slot must be quoted.

— Function: set-slot-value object slot value

This is not a CLOS function, but is meant to mirror slot-value if you don't want to use the cl package's setf function. This function sets the value of slot from object. Unlike oset, the symbol for slot must be quoted.

— Function: slot-makeunbound object slot

This function unbinds slot in object. Referencing an unbound slot can signal an error.

— Function: object-add-to-list object slot item &optional append

In OBJECT's slot, add item to the list of elements. Optional argument append indicates we need to append to the list. If item already exists in the list in slot, then it is not added. Comparison is done with equal through the member function call. If slot is unbound, bind it to the list containing item.

— Function: object-remove-from-list object slot item

In OBJECT's slot, remove occurrences of item. Deletion is done with delete, which deletes by side effect and comparisons are done with equal. If slot is unbound, do nothing.

— Function: with-slots spec-list object &rest body

Bind spec-list lexically to slot values in object, and execute body. This establishes a lexical environment for referring to the slots in the instance named by the given slot-names as though they were variables. Within such a context the value of the slot can be specified by using its slot name, as if it were a lexically bound variable. Both setf and setq can be used to set the value of the slot.

spec-list is of a form similar to let. For example:

            ((VAR1 SLOT1)
             (VARN+1 SLOTN+1))

Where each var is the local variable given to the associated slot. A slot specified without a variable name is given a variable name of the same name as the slot.

          (defclass myclass () (x :initarg 1))
          (setq mc (make-instance 'myclass))
          (with-slots (x) mc x)                      => 1
          (with-slots ((something x)) mc something)  => 1