[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6. Accessing Slots

There are several ways to access slot values in an object. The naming convention and argument order is similar to that found in Emacs Lisp for referencing vectors. The basics for referencing, setting, and calling methods are all accounted for.

Function: oset object slot value

This sets the value behind slot to value in object. oset returns value.

Function: oset-default class slot value

This sets the slot slot in class which is initialized with the :initform tag to value. This will allow a user to set both public and private defaults after the class has been constructed. This function is intrusive, and is offered as a way to allow users to configure the default behavior of packages built with classes the same way setq-default is used for buffer-local variables.

For example, if a user wanted all data-objects (see section 3. 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))

Function: oref object slot

This recalls the value in slot slot in object and returns it. If object is a class, and slot is a class allocated slot, then oref will return that value. If object is a class, and slot is not class allocated, a signal will be thrown.

Function: oref-default object slot
This gets the default value in object's class definition for slot. This can be different from the value returned by oref. object can also be a class symbol or an instantiated object.

These next 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 in.

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

Function: slot-makeunbound object slot
This function unbinds slot in object. Referencing an unbound slot can throw an error.

Function: object-add-to-list object slot &optional append
In OBJECT's SLOT, add ITEM to the pre-existing 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: with-slots entries object forms
Bind entries lexically to the specified slot values in object, and execute forms. In CLOS, it would be possible to set values in OBJECT by using setf to assign to these values, but in Emacs, you may only read the values, or set the local variable to a new value.

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


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by XEmacs shared group account on December, 19 2009 using texi2html 1.65.