make-instance
make-instance Standard Generic Function
Syntax:
make-instance class &rest initargs &key &allow-other-keys → instance
Method Signatures:
make-instance (class standard-class) &rest initargs
make-instance (class symbol) &rest initargs
Arguments and Values:
class—a class, or a symbol that names a class.
initargs—an initialization argument list.
instance—a fresh instance of class class.
Description:
The generic function make-instance creates and returns a new instance of the given class.
If the second of the above methods is selected, that method invokes make-instance on the arguments (find-class class) and initargs.
The initialization arguments are checked within make-instance.
The generic function make-instance may be used as described in Section 7.1 (Object Creation and Initialization).
Exceptional Situations:
If any of the initialization arguments has not been declared as valid, an error of type error is signaled.
See Also:
defclass, class-of, allocate-instance, initialize-instance, Section 7.1 (Object Creation and Initialization)
Expanded Reference: make-instance
Basic Instance Creation
make-instance creates a new instance of a class. The first argument is either a class name (a symbol) or a class object. Additional keyword arguments supply initialization values for slots that have declared :initarg names.
(defclass point ()
((x :initarg :x :accessor point-x :initform 0)
(y :initarg :y :accessor point-y :initform 0)))
(let ((p (make-instance 'point :x 3 :y 4)))
(list (point-x p) (point-y p)))
=> (3 4)
Default Values with :initform
When an initarg is not supplied, the slot receives its :initform default value. Slots without either remain unbound.
(defclass config ()
((host :initarg :host :accessor config-host :initform "localhost")
(port :initarg :port :accessor config-port :initform 8080)
(debug :initarg :debug :accessor config-debug)))
(let ((c (make-instance 'config)))
(list (config-host c) (config-port c)))
=> ("localhost" 8080)
;; Override defaults
(let ((c (make-instance 'config :host "example.com" :port 443)))
(list (config-host c) (config-port c)))
=> ("example.com" 443)
Using a Class Object Instead of a Symbol
You can pass a class object directly to make-instance instead of a symbol.
(defclass point ()
((x :initarg :x :accessor point-x :initform 0)
(y :initarg :y :accessor point-y :initform 0)))
(let* ((cls (find-class 'point))
(p (make-instance cls :x 10 :y 20)))
(list (point-x p) (point-y p)))
=> (10 20)
Creating Instances of Subclasses
make-instance works with inherited slots and default-initargs from superclasses.
(defclass shape ()
((color :initarg :color :accessor shape-color))
(:default-initargs :color :black))
(defclass circle (shape)
((radius :initarg :radius :accessor circle-radius)))
(let ((c (make-instance 'circle :radius 5)))
(list (circle-radius c) (shape-color c)))
=> (5 :BLACK)
(let ((c (make-instance 'circle :radius 5 :color :red)))
(list (circle-radius c) (shape-color c)))
=> (5 :RED)
Interaction with initialize-instance
After allocating the instance, make-instance calls initialize-instance. Custom :after methods on initialize-instance can perform validation or derived slot computation.
(defclass rectangle ()
((width :initarg :width :accessor rect-width)
(height :initarg :height :accessor rect-height)
(area :accessor rect-area)))
(defmethod initialize-instance :after ((r rectangle) &key)
(setf (rect-area r) (* (rect-width r) (rect-height r))))
(let ((r (make-instance 'rectangle :width 3 :height 4)))
(rect-area r))
=> 12