Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Mutator method
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Common Lisp=== In [[Common Lisp Object System]], slot specifications within class definitions may specify any of the <code>:reader</code>, <code>:writer</code> and <code>:accessor</code> options (even multiple times) to define reader methods, setter methods and accessor methods (a reader method and the respective <code>setf</code> method).<ref name="defclass">{{cite web |url=http://www.lispworks.com/documentation/HyperSpec/Body/m_defcla.htm |title=CLHS: Macro DEFCLASS |accessdate=2011-03-29}}</ref> Slots are always directly accessible through their names with the use of <code>with-slots</code> and <code>slot-value</code>, and the slot accessor options define specialized methods that use <code>slot-value</code>.<ref name="defclass-accessors">{{cite web |url=http://www.lispworks.com/documentation/HyperSpec/Body/07_eb.htm |title=CLHS: 7.5.2 Accessing Slots |accessdate=2011-03-29}}</ref> CLOS itself has no notion of properties, although the [[Common Lisp Object System#Metaobject Protocol|MetaObject Protocol]] extension specifies means to access a slot's reader and writer function names, including the ones generated with the <code>:accessor</code> option.<ref name="mop-slot-definitions">{{cite web |url=http://www.lisp.org/mop/concepts.html#slot-definitions |title=MOP: Slot Definitions |accessdate=2011-03-29}}</ref> The following example shows a definition of a student class using these slot options and direct slot access: <syntaxhighlight lang="lisp"> (defclass student () ((name :initarg :name :initform "" :accessor student-name) ; student-name is setf'able (birthdate :initarg :birthdate :initform 0 :reader student-birthdate) (number :initarg :number :initform 0 :reader student-number :writer set-student-number))) ;; Example of a calculated property getter (this is simply a method) (defmethod student-age ((self student)) (- (get-universal-time) (student-birthdate self))) ;; Example of direct slot access within a calculated property setter (defmethod (setf student-age) (new-age (self student)) (with-slots (birthdate) self (setf birthdate (- (get-universal-time) new-age)) new-age)) ;; The slot accessing options generate methods, thus allowing further method definitions (defmethod set-student-number :before (new-number (self student)) ;; You could also check if a student with the new-number already exists. (check-type new-number (integer 1 *))) </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)