1

When an EIEIO class is defined:

(defclass my-test ()
  ((slot1 :initform 0)))

A constructor function of the same name is also defined. However, there's also make-instance, which also makes a new instance of the class.

(let ((a (make-instance 'my-test)) ; -> #s(my-test 0)
      (b (my-test))) ; -> #s(my-test 0)
  (equal a b))
;; -> t

I see that they are different in at least one way: the contructor function prevents one from making an instance of an abstract class, whereas make-instance will ignore it and create an instance anyways.

(defclass my-abstract ()
  ((slot1 :initform 0))
  :abstract t)

(make-instance 'my-abstract) ; -> #s(my-abstract 0)
(my-abstract) ; (error "Class my-abstract is abstract")

Given that, when should one use make-instance, and when should one use the constructor function ((my-test))?

1
  • I imagine that in almost all cases, you want to use the c'tor. The doc (C-h i g(eieio)making new objects) says: To create an object from a class symbol, use ‘make-instance’., but that's about the only exception mentioned.
    – NickD
    Commented Sep 8, 2021 at 5:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.