yet another defclass wrapping macro
Dec. 10th, 2022 06:23 pmAnother entry in the "simplified defclass" reckoning:
I like defstruct, a lot, in terms of its speed and simplicity of interface. But it gets rather miserable when you want to integrate with CLOS IME. So, another stab at it (previous stab was https://github.com/pnathan/defobject ). I think this one is considerably cleaner in stylistics.
(defmacro defstruct* (name &rest slots)
"Provides a DEFSTRUCT interface to a DEFCLASS"
(flet
((accessor-symbol
(sym)
(intern (concatenate 'string (string name) "-" (string sym)))))
(let* ((actual-slots
(etypecase
(car slots)
(string
(cdr slots))
(symbol
slots)
(cons
slots)))
(slot-list
(loop for s in actual-slots
collect
(etypecase s
(symbol
`(,s
:initarg ,s
:accessor ,(accessor-symbol s)))
(cons
`(,(car s)
:initarg ,(car s)
:accessor ,(accessor-symbol (car s))
:initform ,(cadr s))))
)))
`(defclass ,name ()
,slot-list
(:documentation ,(etypecase (car slots)
(string
(car slots))
(symbol
(format nil "The struct defining ~a, containing slots ~{~a~^, ~}" name slots))))
))))
I like defstruct, a lot, in terms of its speed and simplicity of interface. But it gets rather miserable when you want to integrate with CLOS IME. So, another stab at it (previous stab was https://github.com/pnathan/defobject ). I think this one is considerably cleaner in stylistics.