Could any CL’er please explain ‘slots’ in CLOS? I am finding it difficult to understand the part after the slot name. That is in :
(defclass foo () (data1 :initarg foo))
What do the ‘initarg’ and other such similar things mean? I am re-reading manuals. So, I would really appreciate if any of you here could explain it to a layman like me.
Thanks!
Your example is slightly wrong. It has to be:
Notice the added parentheses to indicate a list of slot descriptions.
DEFCLASS takes a list of slots. So with two slots we have:
DATA1 is the name of the slot. Behind that you find pairs of :keyword value.
:INITARG tells you what the parameter for MAKE-INSTANCE is. (make-instance ‘foo :data1arg 10) ; creates the object and sets the slot data1 to 10. Usually you should use a keyword symbol (like :data1arg here).
:INITFORM sets the slot by default, when the object is created. Like in: (make-instance ‘foo) ; creates the object. The slot is set to the value of the initform.
:TYPE specifies the type of the slot’s object.
:DOCUMENTATION is just a string for, well, documentation.
:ACCESSOR specifies a function to read and write the slot.
Note that you can write the pairs in any order and that you can also specify multiple accessor functions. There are also :READER and :WRITER functions.
With CLOS you can specify all that within the DEFCLASS macro. These things are not automatically generated like in defstruct, which has a shorter notation.
The description of DEFCLASS is here: DEFCLASS. Short CLOS Intro.