2

我在访问插槽定义之外的插槽时遇到问题。我可以检查类对象,查看它们的槽定义,甚至获得一些关于槽定义的标准信息。但是,我无法访问有关插槽定义的用户定义信息。

我已经在谷歌上搜索了很长一段时间,最后阅读了CLOS 和 MOP 规范、一些Lisp Cookbook、关于一些MOP 概念以及StackOverflow上的一些 相关 问题 ,但没有多大帮助。我什至阅读了一段SBCL 的 implementations,但无济于事。

从我能够放在一起的部分中,我可以SLOT DEFINITION通过一些功能访问 a 的许多插槽,例如访问using的NAME插槽(这当然很有帮助),但是对于没有的插槽我不能这样做这些功能之一。例如,在.SLOT DEFINITIONCLOSER-MOP:SLOT-DEFINITION-NAMEREFERENCESMITODEFCLASS

这是一个最小的工作示例:

(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-VALUEslot 时,我们得到一个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 实现。

4

1 回答 1

4

插槽名称是符号,因此在使用SLOT-VALUE/时包很重要WITH-SLOTS。在这种情况下,引用插槽似乎由包中的内部符号命名MITO.CLASS.COLUMN

检查器不显示包名(因为它很少需要),但是您可以看到插槽定义是 type MITO.DAO.COLUMN:DAO-TABLE-COLUMN-CLASS,因此您可以使用CLOSER-MOP:CLASS-SLOTS来查找插槽定义:

CL-USER> (closer-mop:class-slots (find-class 'MITO.DAO.COLUMN:DAO-TABLE-COLUMN-CLASS))
(#<SB-MOP:STANDARD-EFFECTIVE-SLOT-DEFINITION SB-PCL::SOURCE>
 #<SB-MOP:STANDARD-EFFECTIVE-SLOT-DEFINITION SB-PCL::NAME>
 #<SB-MOP:STANDARD-EFFECTIVE-SLOT-DEFINITION SB-PCL::INITFORM>
 #<SB-MOP:STANDARD-EFFECTIVE-SLOT-DEFINITION SB-PCL::INITFUNCTION>
 #<SB-MOP:STANDARD-EFFECTIVE-SLOT-DEFINITION SB-PCL::INITARGS>
 #<SB-MOP:STANDARD-EFFECTIVE-SLOT-DEFINITION SB-PCL::%TYPE>
 #<SB-MOP:STANDARD-EFFECTIVE-SLOT-DEFINITION SB-PCL::%DOCUMENTATION>
 #<SB-MOP:STANDARD-EFFECTIVE-SLOT-DEFINITION SB-PCL::%CLASS>
 #<SB-MOP:STANDARD-EFFECTIVE-SLOT-DEFINITION SB-PCL::READERS>
 #<SB-MOP:STANDARD-EFFECTIVE-SLOT-DEFINITION SB-PCL::WRITERS>
 #<SB-MOP:STANDARD-EFFECTIVE-SLOT-DEFINITION SB-PCL::ALLOCATION>
 #<SB-MOP:STANDARD-EFFECTIVE-SLOT-DEFINITION SB-PCL::ALLOCATION-CLASS>
 #<SB-MOP:STANDARD-EFFECTIVE-SLOT-DEFINITION MITO.CLASS.COLUMN::COL-TYPE>
 #<SB-MOP:STANDARD-EFFECTIVE-SLOT-DEFINITION MITO.CLASS.COLUMN::REFERENCES>   ;<-- HERE
 #<SB-MOP:STANDARD-EFFECTIVE-SLOT-DEFINITION MITO.CLASS.COLUMN::PRIMARY-KEY>
 #<SB-MOP:STANDARD-EFFECTIVE-SLOT-DEFINITION MITO.CLASS.COLUMN::GHOST>
 #<SB-MOP:STANDARD-EFFECTIVE-SLOT-DEFINITION MITO.DAO.COLUMN::INFLATE>
 #<SB-MOP:STANDARD-EFFECTIVE-SLOT-DEFINITION MITO.DAO.COLUMN::DEFLATE>)

或者,您可以尝试(apropos "references")查看所有带有此类符号的软件包:

CL-USER> (apropos "references")
REFERENCES
:HAS-EXTERNAL-REFERENCES-P (bound)
:REFERENCES (bound)
MITO.CLASS.COLUMN::REFERENCES  ;<-- HERE
MITO.CLASS.COLUMN:TABLE-COLUMN-REFERENCES (fbound)
MITO.CLASS.COLUMN:TABLE-COLUMN-REFERENCES-COLUMN (fbound)
...
于 2019-02-14T21:08:09.737 回答