Next: , Previous: , Up: Top   [Contents][Index]

4 Making New Objects

Suppose we have a simple class is defined, such as:

(defclass record ()
   ( ) "Doc String")

It is now possible to create objects of that class type.

Calling defclass has defined two new functions. One is the constructor record, and the other is the predicate, record-p.

Function: record object-name &rest slots

This creates and returns a new object. This object is not assigned to anything, and will be garbage collected if not saved. This object will be given the string name object-name. There can be multiple objects of the same name, but the name slot provides a handy way to keep track of your objects. slots is just all the slots you wish to preset. Any slot set as such will not get its default value, and any side effects from a slot’s :initform that may be a function will not occur.

An example pair would appear simply as :value 1. Of course you can do any valid Lispy thing you want with it, such as :value (if (boundp 'special-symbol) special-symbol nil)

Example of creating an object from a class:

(record "test" :value 3 :reference nil)

To create an object from a class symbol, use make-instance.

Function: make-instance class &rest initargs

Make a new instance of class based on initargs. class is a class symbol. For example:

  (make-instance 'foo)

initargs is a property list with keywords based on the :initarg for each slot. For example:

  (make-instance 'foo :slot1 value1 :slotN valueN)

Compatibility note:

If the first element of initargs is a string, it is used as the name of the class.

In EIEIO, the class’ constructor requires a name for use when printing. make-instance in CLOS doesn’t use names the way Emacs does, so the class is used as the name slot instead when initargs doesn’t start with a string.

Next: , Previous: , Up: Top   [Contents][Index]