Warm tip: This article is reproduced from stackoverflow.com, please click
clos common-lisp let sbcl

Why does calling `make-instance` in `let` work differently?

发布于 2020-03-27 10:17:17

I'm exploring some possibilities of Common Lisp syntax and I wanted to make an :around method on make-instance to return some arbitrary value in some cases. For sake of simplicity, let it be nil, when I don't pass a needed argument. And it works, but not when calling in let:

(defclass foo ()
  ((bar :initarg := :initform '())))

(defmethod make-instance :around ((type (eql 'foo)) &key =)
  (if (not =) nil (call-next-method)))

(print (make-instance 'foo))    ;; => NIL

(print (let ((x (make-instance 'foo))) x)) ;; => #<FOO {10037EEDF3}> 

Can somebody explain this situation? Why is that so? Does SBCL try to be smart or is it actually a standard thing to be done? I know that I can work around it by using apply:

(print (let ((x (apply #'make-instance (list 'foo)))) x)) ;; => NIL

But I don't want to rely on this sort of workaround. Actually I can use a regular function for that and it's OK, but I want to understand why it works this way and if this behaviour can be disabled.

Questioner
Bartek Lew
Viewed
40
Rainer Joswig 2019-06-16 18:43

Looks like one of the optimization attempts for MAKE-INSTANCE and constant class names in SBCL (-> CTOR)...

This seems to work:

(defmethod make-instance :around ((class (eql (find-class 'foo)))
                                  &rest initargs
                                  &key =
                                  &allow-other-keys)
  (declare (ignorable initargs))
  (if (not =) nil (call-next-method)))

But probably useful to ask a SBCL expert or file a bug report...