Skip to main content

allocate-instance

allocate-instance Standard Generic Function

Syntax:

allocate-instance class &rest initargs &key &allow-other-keys → new-instance

Method Signatures:

allocate-instance (class standard-class) &rest initargs

allocate-instance (class structure-class) &rest initargs

Arguments and Values:

class—a class.

initargs—a list of keyword/value pairs (initialization argument names and values). new-instance—an object whose class is class.

Description:

The generic function allocate-instance creates and returns a new instance of the class, without initializing it. When the class is a standard class, this means that the slots are unbound; when the class is a structure class, this means the slotsvalues are unspecified.

The caller of allocate-instance is expected to have already checked the initialization arguments.

The generic function allocate-instance is called by make-instance, as described in Section 7.1 (Object Creation and Initialization).

See Also:

defclass, make-instance, class-of, Section 7.1 (Object Creation and Initialization)

Notes:

The consequences of adding methods to allocate-instance is unspecified. This capability might be added by the Metaobject Protocol.

Expanded Reference: allocate-instance

Understanding allocate-instance

allocate-instance creates a raw, uninitialized instance of a class. All slots are unbound. It is called internally by make-instance before initialize-instance. Normally you do not call it directly.

(defclass point ()
((x :initarg :x :accessor point-x)
(y :initarg :y :accessor point-y)))

;; allocate-instance creates an instance with unbound slots
(let ((p (allocate-instance (find-class 'point))))
(list (slot-boundp p 'x) (slot-boundp p 'y)))
=> (NIL NIL)

Difference from make-instance

Unlike make-instance, allocate-instance does not process initargs, initforms, or call initialize-instance.

(defclass widget ()
((label :initarg :label :initform "default" :accessor widget-label)))

;; make-instance applies the initform
(widget-label (make-instance 'widget))
=> "default"

;; allocate-instance leaves the slot unbound
(let ((w (allocate-instance (find-class 'widget))))
(slot-boundp w 'label))
=> NIL