While making classes on CLOS I've met the same pattern multiple times:
(defclass class-name ()
((field-1
:initarg field-1
:initform some-value
:accessor field-1)
(field-2
:initarg field-2
:initform another-value
:accessor field-2)
(...)
(field-n
:initarg field-n
:initform n-value
:accessor field-n)))
(whether this is good design is something I'll learn with time)
I've tried to tackle this with a macro so I could call, say:
(defclass-with-accessors 'class-name
(('field-1 some-value)
('field-2 another-value)
(...)
('field-n n-value)))
My first tackle (ignoring hygiene for now) was to split into two macros: one to make each field, other to make the class itself.
The macro to make the accessor fields seems to be correct:
(defmacro make-accessor-field (name form)
`(,name
:initarg ,(make-keyword name)
:initform ,form
:accessor ,name))
But I'm not getting the main macro right. My first attempt was:
(defmacro defclass-with-accessors (name body)
`(defclass ,name () \(
,(loop for my-slot in body collect
(make-accessor-field (car my-slot) (cadr my-slot)))))
But this isn't valid, SBCL giving me the following error at the defmacro evaluation:
; in: DEFMACRO DEFCLASS-WITH-ACCESSORS
; (MAKE-ACCESSOR-FIELD (CAR MY-SLOT) (CADR MY-SLOT))
;
; caught ERROR:
; during macroexpansion of (MAKE-ACCESSOR-FIELD (CAR MY-SLOT) (CADR MY-SLOT)).
; Use *BREAK-ON-SIGNALS* to intercept.
;
; The value (CAR MY-SLOT)
; is not of type
; (OR (VECTOR CHARACTER) (VECTOR NIL) BASE-STRING SYMBOL CHARACTER).
;
; compilation unit finished
; caught 1 ERROR condition
STYLE-WARNING:
redefining COMMON-LISP-USER::DEFCLASS-WITH-ACCESSORS in DEFMACRO
What's happening exactly? How can the compiler tell the type of (car slot) when slot isn't even defined? How can I proceed to correctly define this macro?