Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using dot notation to access CLOS slots

When accessing class slots, instead of writing

(defmethod get-name ((somebody person) (slot-value somebody 'name))

is it possible to use the dot notation aka C++, namely

(defmethod get-name ((somebody person) somebody.name) ?

Otherwise, when there are many slot operations in a method, (slot-value... creates a lot of boilerplate code.

I have figured out the answer today and I am just posting it as a Q&A, but if there are better solutions or there are problems I should expect with my solution, feel free to add new answers or comments.

like image 206
Andrei Avatar asked Feb 28 '26 23:02

Andrei


2 Answers

The library access provides a dot notation reader macro for accessing slots (and hash-tables and other things). After enabling the reader macro by calling (access:enable-dot-syntax) you'll able to use #D. to access a slot name with the dot syntax popular in other languages.

(defclass person ()
  ((name :initarg :name :reader name)))

CL-USER> (access:enable-dot-syntax)
; No values
CL-USER> (defvar *foo* (make-instance 'person :name "John Smith"))
*FOO*
CL-USER> #D*foo*
#<PERSON #x302001F1E5CD>
CL-USER> #D*foo*.name
"John Smith"

There is also a with-dot macro if you don't want to use a reader macro

CL-USER> (access:with-dot () *foo*.name)
"John Smith"
like image 50
PuercoPop Avatar answered Mar 03 '26 19:03

PuercoPop


You should not write accessors by hand, nor use slot-value (outside of object lifecycle functions, where the accessors may not have been created yet). Use the class slot options instead:

(defclass foo ()
  ((name :reader foo-name
         :initarg :name)
   (bar :accessor foo-bar
        :initarg :bar)))

Now you can use the named accessors:

(defun example (some-foo new-bar)
  (let ((n (foo-name some-foo))
        (old-bar (foo-bar some-foo)))
    (setf (foo-bar some-foo) new-bar)
    (values n old-bar)))

Often, you want your classes to be "immutable", you'd use :reader instead of :accessor then, which only creates the reader, not the setf expansion.

like image 25
Svante Avatar answered Mar 03 '26 19:03

Svante



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!