首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何访问Common中Defclass的槽的:Documentation

如何访问Common中Defclass的槽的:Documentation
EN

Stack Overflow用户
提问于 2014-01-26 17:56:27
回答 1查看 591关注 0票数 2

下面是我如何实例化我的Defclass和相关的Defmethod和Def参数

代码语言:javascript
复制
(defvar *account-numbers* 0)

 (defclass bank-account ()
 ((customer-name
 :initarg :customer-name
 :initform (error "Must supply a customer name.")
 :accessor customer-name
 :documentation "Customer's name")
 (balance
 :initarg :balance
 :initform 0
 :reader balance
 :documentation "Current account balance")
  (account-number
   :initform (incf *account-numbers*)
   :reader account-number
   :documentation "Account number, unique within a bank.")
  (account-type
   :reader account-type
   :documentation "Type of account, one of :gold, :silver, or :bronze.")))

(defmethod initialize-instance :after ((account bank-account)
                   &key opening-bonus-percentage)
(when opening-bonus-percentage
  (incf (slot-value account 'balance)
  (* (slot-value account 'balance) (/ opening-bonus-percentage 100)))))

(defparameter *account* (make-instance
         'bank-account
         :customer-name "Ed Monney"
         :balance 1000000000
         :opening-bonus-percentage 5))

我试图访问任何上述时隙的:文档时隙值,但无法在我正在阅读的书或谷歌中找到信息.

我的尝试包括

代码语言:javascript
复制
 (documentation *account* 'balance)

得到了这个错误

警告:不支持的文档:为类型银行帐户的对象键入余额

我试过了

代码语言:javascript
复制
(slot-value bank-account ('balance :documentation))

我得到了

代码语言:javascript
复制
The variable BANK-ACCOUNT is unbound.
[Condition of type UNBOUND-VARIABLE]

我尝试了所有我能想到的slot-value 'balance :documentation 'documentation bank-account and *account*的其他变体,但是只是得到了许多不同的错误,任何关于学习如何访问defclass插槽的文档的帮助都是非常感谢的。

编辑:@Rainer,它似乎只在我进入repl的defclass之后才能工作……我希望有一种方法,如果我在库中有一个集合defclass或者其他什么东西,我就可以运行一个命令并访问这个文档。。但是,如果在defclass ....I之后我在repl上运行了其他东西,那么当我运行这4行代码时会出错.我在运行初始化实例和我的error....could帐户后尝试了一些类似于(文档(时隙值帐户'balance) t)的方法,但是得到了error....could,您建议了一种使文档更容易访问的方法。

EN

回答 1

Stack Overflow用户

发布于 2014-01-26 19:51:50

这在通用Lisp标准中没有定义。不幸的是,这也不是初学者的领地。

实现可以提供一种方法来访问插槽的文档字符串。

LispWorks示例

代码语言:javascript
复制
CL-USER 23 > (defclass foo ()
               ((bar :documentation "this is slot bar in class foo")))
#<STANDARD-CLASS FOO 40200032C3>

CL-USER 24 > (class-slots *)
(#<STANDARD-EFFECTIVE-SLOT-DEFINITION BAR 4020004803>)

CL-USER 25 > (first *)
#<STANDARD-EFFECTIVE-SLOT-DEFINITION BAR 4020004803>

CL-USER 26 > (documentation * 'slot-definition)
"this is slot bar in class foo"

它也适用于Clozure。

对于SBCL,它的工作方式略有不同。

代码语言:javascript
复制
* (defclass foo ()
     ((bar :documentation "this is slot bar in class foo")))

#<STANDARD-CLASS FOO>


* (sb-mop:finalize-inheritance *)

NIL


* (sb-mop::class-slots **)

(#<SB-MOP:STANDARD-EFFECTIVE-SLOT-DEFINITION BAR>)


* (first *)

#<SB-MOP:STANDARD-EFFECTIVE-SLOT-DEFINITION BAR>


* (documentation * t)

"this is slot bar in class foo"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21367083

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档