user2904561
user2904561

Reputation:

How do i access the :Documentation string of a slot of a Defclass in Common lisp

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

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....

My attempts include

 (documentation *account* 'balance)

got this error

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

I tried

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

I got

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

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

Edit: @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.

Upvotes: 2

Views: 724

Answers (1)

Rainer Joswig
Rainer Joswig

Reputation: 139251

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 example:

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"

It also works in Clozure CL.

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"

Upvotes: 1

Related Questions