Next: , Previous: Inheritance, Up: Building Classes

3.2 Slot Options

The slot-list argument to defclass is a list of elements where each element defines one slot. Each slot is a list of the form

       (SLOT-NAME :TAG1 ATTRIB-VALUE1
                  :TAG2 ATTRIB-VALUE2
                  :TAGN ATTRIB-VALUEN)

where SLOT-NAME is a symbol that will be used to refer to the slot. :TAG is a symbol that describes a feature to be set on the slot. ATTRIB-VALUE is a lisp expression that will be used for :TAG.

Valid tags are:

:initarg
A symbol that can be used in the argument list of the constructor to specify a value for the new instance being created.

A good symbol to use for initarg is one that starts with a colon :.

The slot specified like this:

            (myslot :initarg :myslot)

could then be initialized to the number 1 like this:

            (myobject "name" :myslot 1)

See Making New Objects.

:initform
A expression used as the default value for this slot.

If :initform is left out, that slot defaults to being unbound. It is an error to reference an unbound slot, so if you need slots to always be in a bound state, you should always use an :initform specifier.

Use slot-boundp to test if a slot is unbound (see Predicates). Use slot-makeunbound to set a slot to being unbound after giving it a value (see Accessing Slots).

The value passed to initform is automatically quoted. Thus,

          :initform (1 2 3)

appears as the specified list in the default object. A symbol that is a function like this:

          :initform +

will set the initial value as that symbol.

After a class has been created with defclass, you can change that default value with oset-default. Accessing Slots.

:type
An unquoted type specifier used to validate data set into this slot. See Type Predicates. Here are some examples:
symbol
A symbol.
number
A number type
my-class-name
An object of your class type.
(or null symbol)
A symbol, or nil.

:allocation
Either :class or :instance (defaults to :instance) used to specify how data is stored. Slots stored per instance have unique values for each object. Slots stored per class have shared values for each object. If one object changes a :class allocated slot, then all objects for that class gain the new value.
:documentation
Documentation detailing the use of this slot. This documentation is exposed when the user describes a class, and during customization of an object.
:accessor
Name of a generic function which can be used to fetch the value of this slot. You can call this function later on your object and retrieve the value of the slot.

This options is in the CLOS spec, but is not fully compliant in EIEIO.

:writer
Name of a generic function which will write this slot.

This options is in the CLOS spec, but is not fully compliant in EIEIO.

:reader
Name of a generic function which will read this slot.

This options is in the CLOS spec, but is not fully compliant in EIEIO.

:custom
A custom :type specifier used when editing an object of this type. See documentation for defcustom for details. This specifier is equivalent to the :type spec of a defcustom call.

This options is specific to Emacs, and is not in the CLOS spec.

:label
When customizing an object, the value of :label will be used instead of the slot name. This enables better descriptions of the data than would usually be afforded.

This options is specific to Emacs, and is not in the CLOS spec.

:group
Similar to defcustom's :group command, this organizes different slots in an object into groups. When customizing an object, only the slots belonging to a specific group need be worked with, simplifying the size of the display.

This options is specific to Emacs, and is not in the CLOS spec.

:printer
This routine takes a symbol which is a function name. The function should accept one argument. The argument is the value from the slot to be printed. The function in object-write will write the slot value out to a printable form on standard-output.

The output format MUST be something that could in turn be interpreted with read such that the object can be brought back in from the output stream. Thus, if you wanted to output a symbol, you would need to quote the symbol. If you wanted to run a function on load, you can output the code to do the construction of the value.

:protection
When using a slot referencing function such as slot-value, and the value behind slot is private or protected, then the current scope of operation must be within a method of the calling object.

Valid values are:

:public
Access this slot from any scope.
:protected
Access this slot only from methods of the same class or a child class.
:private
Access this slot only from methods of the same class.

This options is specific to Emacs, and is not in the CLOS spec.