http://mop.lisp.se/concepts.html说:
举例:
(defclass sst (plane)
((mach mag-step 2
locator sst-mach
locator mach-location
:reader mach-speed
:reader mach))
(:metaclass faster-class)
(another-option foo bar))
但是当我尝试:
(defclass a () ((x my-option 123)))
SBCL编译时出现错误:
那么问题来了。如何在插槽定义中添加其他属性(例如“my-option”)?
最佳答案
一个实现可以做到这一点。但是用户不能添加随机属性。如果Common Lisp实现支持元对象协议(protocol),则可以通过自定义元类将其添加。但这意味着,还需要提供一种计算插槽等的方法。
那是先进的Lisp。 《元对象协议(protocol)的艺术》一书在第3章“扩展语言”中有一个示例。
一个简单的示例(在LispWorks中有效):
(defclass foo-meta-class (standard-class) ())
(defclass foo-standard-direct-slot-definition (standard-direct-slot-definition)
((foo :initform nil :initarg :foo)))
(defclass foo-standard-effective-slot-definition (standard-effective-slot-definition)
((foo :initform nil :initarg :foo)))
(defmethod clos:direct-slot-definition-class ((class foo-meta-class) &rest initargs)
(find-class 'foo-standard-direct-slot-definition))
(defmethod clos:effective-slot-definition-class ((class foo-meta-class) &rest initargs)
(find-class 'foo-standard-effective-slot-definition))
让我们在用户定义的类中使用它:
(defclass foo ()
((a :initarg :a :foo :bar))
(:metaclass foo-meta-class))
然后,插槽定义对象将具有内容为
foo
的插槽bar
。CL-USER 10 > (find-class 'foo)
#<FOO-META-CLASS FOO 42200995AB>
CL-USER 11 > (class-direct-slots *)
(#<FOO-STANDARD-DIRECT-SLOT-DEFINITION A 42200B4C7B>)
CL-USER 12 > (describe (first *))
#<FOO-STANDARD-DIRECT-SLOT-DEFINITION A 42200B4C7B> is a FOO-STANDARD-DIRECT-SLOT-DEFINITION
FOO :BAR
READERS NIL
WRITERS NIL
NAME A
INITFORM NIL
INITFUNCTION NIL
TYPE T
FLAGS 1
INITARGS (:A)
ALLOCATION :INSTANCE
DOCUMENTATION-SLOT NIL
如果该属性应该具有任何实际含义,那么显然还有更多内容。