我在访问插槽定义之外的插槽时遇到问题。我可以检查类对象,查看它们的槽定义,甚至获得一些关于槽定义的标准信息。但是,我无法访问有关插槽定义的用户定义信息。
我已经在谷歌上搜索了很长一段时间,最后阅读了CLOS 和 MOP 规范、一些Lisp Cookbook、关于一些MOP 概念以及StackOverflow上的一些 相关 问题 ,但没有多大帮助。我什至阅读了一段SBCL 的 implementations,但无济于事。
从我能够放在一起的部分中,我可以SLOT DEFINITION
通过一些功能访问 a 的许多插槽,例如访问using的NAME
插槽(这当然很有帮助),但是对于没有的插槽我不能这样做这些功能之一。例如,在.SLOT DEFINITION
CLOSER-MOP:SLOT-DEFINITION-NAME
REFERENCES
MITO
DEFCLASS
这是一个最小的工作示例:
(load "~/quicklisp/setup.lisp")
;;;; I'll use MITO because its classes have a funny REFERENCES slot
(quicklisp:quickload :mito)
;;;; I find CLOSER-MOP functions easier to use than
;;;; implementation-specific functions
(quicklisp:quickload :closer-mop)
;;;; Creates a few dummy classes
(defclass example ()
((one-slot :col-type (:varchar 50)
:accessor one-slot-accessor))
(:metaclass mito:dao-table-class)
(:documentation "An example class."))
(defclass another-example ()
((normal-slot :col-type (:varchar 50)
:accessor normal-slot-accessor)
(example-reference :references (example id)
:reader example-reference-accessor))
(:metaclass mito:dao-table-class)
(:documentation "Another example class which references `EXAMPLE' class."))
;;;; Now try to see what's inside those classes
(let* ((class (find-class 'another-example))
(slots (closer-mop:class-direct-slots class))
(slot-i-am-interested (second slots)))
(inspect slot-i-am-interested) ; Oh, apparently we have a REFERENCES slot
;; Why can't SLOT-VALUE access the REFERENCES slot?
(slot-value slot-i-am-interested 'references))
请注意,我没有访问任何数据库或任何类似的东西,尽管我正在使用MITO
(如果我不使用任何自定义插槽,我认为我不会遇到这个问题,比如提供的REFERENCES
那个MITO
)。这只是简单的 CLOS/MOP 操作。
通常的输出类似于(确切的输出取决于您的 Common Lisp 实现):
user@linuxstudio:~/dev/lisp/slot-question-so$ sbcl --script example.lisp
To load "mito":
Load 1 ASDF system:
mito
; Loading "mito"
....
To load "closer-mop":
Load 1 ASDF system:
closer-mop
; Loading "closer-mop"
The object is a STANDARD-OBJECT of type MITO.DAO.COLUMN:DAO-TABLE-COLUMN-CLASS.
0. SOURCE: #S(SB-C:DEFINITION-SOURCE-LOCATION
:NAMESTRING "/home/user/dev/lisp/slot-question-so/example.lisp"
:INDICES 163840)
1. NAME: EXAMPLE-REFERENCE
2. INITFORM: NIL
3. INITFUNCTION: NIL
4. INITARGS: (:EXAMPLE-REFERENCE)
5. %TYPE: T
6. %DOCUMENTATION: NIL
7. %CLASS: #<MITO.DAO.TABLE:DAO-TABLE-CLASS COMMON-LISP-USER::ANOTHER-EXAMPLE>
8. READERS: (EXAMPLE-REFERENCE-ACCESSOR)
9. WRITERS: NIL
10. ALLOCATION: :INSTANCE
11. ALLOCATION-CLASS: NIL
12. COL-TYPE: NIL
13. REFERENCES: (EXAMPLE ID)
14. PRIMARY-KEY: NIL
15. GHOST: NIL
16. INFLATE: "unbound"
17. DEFLATE: "unbound"
> q
所以,显然,我们确实有一个REFERENCES
插槽。但是,之后INSPECT
,当我们尝试到SLOT-VALUE
slot 时,我们得到一个SLOT-MISSING
错误(仅显示回溯的第一行):
Unhandled SIMPLE-ERROR in thread #<SB-THREAD:THREAD "main thread" RUNNING
{10005E85B3}>:
When attempting to read the slot's value (slot-value), the slot REFERENCES is
missing from the object
#<MITO.DAO.COLUMN:DAO-TABLE-COLUMN-CLASS COMMON-LISP-USER::EXAMPLE-REFERENCE>.
Backtrace for: #<SB-THREAD:THREAD "main thread" RUNNING {10005E85B3}>
0: (SB-DEBUG::DEBUGGER-DISABLED-HOOK #<SIMPLE-ERROR "~@<When attempting to ~A, the slot ~S is missing from the ~
object ~S.~@:>" {1004B1B803}> #<unused argument> :QUIT T)
1: (SB-DEBUG::RUN-HOOK *INVOKE-DEBUGGER-HOOK* #<SIMPLE-ERROR "~@<When attempting to ~A, the slot ~S is missing from the ~
object ~S.~@:>" {1004B1B803}>)
2: (INVOKE-DEBUGGER #<SIMPLE-ERROR "~@<When attempting to ~A, the slot ~S is missing from the ~
object ~S.~@:>" {1004B1B803}>)
3: (ERROR "~@<When attempting to ~A, the slot ~S is missing from the ~
object ~S.~@:>" "read the slot's value (slot-value)" REFERENCES #<MITO.DAO.COLUMN:DAO-TABLE-COLUMN-CLASS COMMON-LISP-USER::EXAMPLE-REFERENCE>)
4: ((:METHOD SLOT-MISSING (T T T T)) #<unused argument> #<MITO.DAO.COLUMN:DAO-TABLE-COLUMN-CLASS COMMON-LISP-USER::EXAMPLE-REFERENCE> REFERENCES SLOT-VALUE NIL) [fast-method]
5: ((LAMBDA (SB-PCL::OBJECT) :IN SB-PCL::SLOT-MISSING-INFO) #<MITO.DAO.COLUMN:DAO-TABLE-COLUMN-CLASS COMMON-LISP-USER::EXAMPLE-REFERENCE>)
6: ((LAMBDA NIL :IN "/home/user/dev/lisp/slot-question-so/example.lisp"))
那么如何访问“插槽” REFERENCES
?这真的是一个插槽吗?如果没有,我该如何访问它?为什么在这种情况下一个简单的SLOT-VALUE
不起作用?
如果我想了解更多有关此主题的信息,您能否指出一些有关此主题的文档以提供更多信息?
SBCL 1.4.5.debian
如果这可能很重要,我将其用作我的 lisp 实现。