我如何在Common Lisp中访问Defclass插槽的:Documentation字符串 [英] How do i access the :Documentation string of a slot of a Defclass in Common lisp

查看:44
本文介绍了我如何在Common Lisp中访问Defclass插槽的:Documentation字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是我如何实例化我的Defclass以及相关的Defmethod和Defparameter

Ok here is How i instantiate my Defclass and related Defmethod and Defparameter

(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))

我正在尝试访问任何上述插槽的:documentation插槽值,但无法在我正在阅读的书或Google中找到信息.

I'm trying to access the :documentation slot-value of any said slot but have not been able to find info in the book i'm reading nor google....

我的尝试包括

 (documentation *account* 'balance)

遇到此错误

警告:不受支持的文档:BANK-ACCOUNT类型的对象的类型为BALANCE

WARNING: unsupported DOCUMENTATION: type BALANCE for object of type BANK-ACCOUNT

我尝试了

(slot-value bank-account ('balance :documentation))

我知道了

The variable BANK-ACCOUNT is unbound.
[Condition of type UNBOUND-VARIABLE]

我尝试了我能想到的所有其他变体 slot-value'balance:documentation'documentation bank-account和* account * 我可以想到,但是得到了很多不同的错误,对您有所帮助学习如何访问defclass插槽的:documentation非常感谢

I tried every other variation I could think of slot-value 'balance :documentation 'documentation bank-account and *account* I can think of but just got a lot of different errors any help on learning how to access the :documentation of a defclass slot is much appreciated

@Rainer Joswig似乎只有在我代表进入defclass之后才可以工作...我希望找到一种方法,如果我在库中有一个defclass或其他东西,我可以运行命令并访问文档..他们的方式是您发布的,但是如果我在defclass之后在repl上运行其他内容....当我运行这4行代码时出现错误....我尝试了类似(documentation(slot-value account'balance)t)如我的帖子所述,运行了我的initialize-instance和defparam 帐户后,却出现错误....您能建议一种简化文档的方法吗访问.

@Rainer Joswig that seems to work only right after i entered the defclass at the repl...I was hoping for a way that , if I had a set defclass in a library or something I could just run a command and access the doc. . They way you posted though if i run something else at the repl after the defclass ....I get an error when i run those 4 lines of code....I tried something like (documentation (slot-value account 'balance) t) after i've run my initialize-instance and my defparam account as in my post but get error....could you suggest a way to make the documentation easier to access.

推荐答案

Common Lisp标准中未定义.不幸的是,这也不是初学者的领地.

This is not defined in the Common Lisp standard. Unfortunately this is also not beginners territory.

实施可以提供一种访问插槽文档字符串的方法.

Implementations may provide a way to access the documentation string of a slot.

LispWorks示例:

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 CL中使用.

It also works in Clozure CL.

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

For SBCL it works slightly different.

* (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"

这篇关于我如何在Common Lisp中访问Defclass插槽的:Documentation字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆