考虑这段代码:
(defclass test () ((test :initform nil :accessor test)))
#<STANDARD-CLASS TEST>
(defvar *test* (make-instance 'test))
*TEST*
Run Code Online (Sandbox Code Playgroud)
而这个测试:
(funcall #'test *test*)
nil
Run Code Online (Sandbox Code Playgroud)
人们会期望这是有效的:
(setf (funcall #'test *test*) 123)
Run Code Online (Sandbox Code Playgroud)
同样的
(setf (test *test*) 123)
123
Run Code Online (Sandbox Code Playgroud)
但结果如下:
; in: LAMBDA NIL
; (FUNCALL #'(SETF FUNCALL) #:NEW1175 #:TMP1177 #:TMP1176)
; ==>
; (SB-C::%FUNCALL #'(SETF FUNCALL) #:NEW1175 #:TMP1177 #:TMP1176)
;
; caught WARNING:
; The function (SETF FUNCALL) is undefined, and its name is reserved by ANSI CL
; so that even if it were defined later, the code doing so would not be portable.
;
; compilation unit finished
; Undefined function:
; (SETF FUNCALL)
; caught 1 WARNING condition
Run Code Online (Sandbox Code Playgroud)
为什么它不起作用,我该如何解决它?
我用SBCL和CLISP测试了它,结果相同.
SETF是一种特殊形式(请参阅http://www.lispworks.com/documentation/HyperSpec/Body/05_aa.htm,以解释它的规范部分).你的第二个例子是有效的,因为lisp实现在(test *test*)语法上解释.
要了解发生了什么,请查看此会话:
This is SBCL 1.0.56.0.debian, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.
SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses. See the CREDITS and COPYING files in the
distribution for more information.
* (defclass test () ((test :initform nil :accessor test)))
#<STANDARD-CLASS TEST>
* (defvar *test* (make-instance 'test))
*TEST*
* (macroexpand '(setf (test *test*) 123))
(LET* ((#:*TEST*606 *TEST*))
(MULTIPLE-VALUE-BIND (#:NEW605)
123
(FUNCALL #'(SETF TEST) #:NEW605 #:*TEST*606)))
T
* #'(setf test)
#<STANDARD-GENERIC-FUNCTION (SETF TEST) (1)>
* (macroexpand '(setf (funcall #'test *test*) 123))
(LET* ((#:G609 #'TEST) (#:*TEST*608 *TEST*))
(MULTIPLE-VALUE-BIND (#:NEW607)
123
(FUNCALL #'(SETF FUNCALL) #:NEW607 #:G609 #:*TEST*608)))
T
Run Code Online (Sandbox Code Playgroud)
请注意,第一个宏扩展抓取#'(setf test),它是由您的defclass调用自动定义的编写器函数.第二个盲目转换为#'(setf funcall),不存在(因此错误).
回答你的"我该如何解决它?" 问题,我们可能需要更多地了解您正在尝试做什么.例如,您可以使用类似的东西(setf (slot-value object slot-name))来允许您以编程方式选择插槽.