Next: , Previous: Building Classes, Up: Top

4 Making New Objects

Suppose we have defined a simple class, such as:

     (defclass my-class ()
        ( ) "Doc String")

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

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

— Function: my-class 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:

          (my-class :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)