eieio: Accessing Slots
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 ((elisp)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 value for the class-allocated SLOT in CLASS to
VALUE.
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”.
-- Macro: oref-default class slot
Get the value of the class-allocated SLOT from CLASS.
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 the setter for ‘slot-value’
used by the ‘setf’ macro. 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)
SLOT2
SLOTN
(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 :initform 1))
(setq mc (make-instance 'myclass))
(with-slots (x) mc x) => 1
(with-slots ((something x)) mc something) => 1