I have a class in Common Lisp:
(defclass my-cool-class() ((variable1 :initarg :variable1 :accessor variable1 :initform (error "Must supply value to variable1")) (variable2 :initarg :variable2 :accessor variable2 :initform (error "Must supply value to variable2"))
I wanted to create a macro that would simplify this input redundancy
(defmacro make-slot (slot-name) `(slot-name :initarg :,slot-name :accessor :,slot-name :initform (error "Must supply value")))
In the end, I would like (defclass my-cool-class () (make-slots' (foo bar baz)) and automatically get foo, bar and baz as time intervals.
But, when I went to do macroexpand-1 in make-slot, boy, I realized that I had read errors.
The first was the "illegal terminating character after the colon ...", and then it kept going.
SBCL 1.0.37.
edit: the examples are syntactically correct on the system, I did some editing before I copied.
Six months later -
(defun build-var (classname var) (list var :initform nil :accessor (intern (concatenate 'string (string classname) "-" (string var))) :initarg (intern (string var) :keyword))) (defun build-varlist (classname varlist) (loop for var in varlist collect (build-var classname var))) (defmacro defobject (name &rest varlist) "Defines a class with a set of behavior. Variables are accessed by name-varname. (defobject classname v1 v2 v3) " `(defclass ,name () ,(build-varlist name varlist))):
Two and a half years later.
I discovered a six month old code in the wild elsewhere. Although I am flattered, it also reminds me of that.
If you like this idea, I save this code in the garden: https://github.com/pnathan/defobject . As before, its goal is to create CLOS classes with minimal repetitive typing. A similar system exists as DEFCLASS-STAR. Interested parties are encouraged to read them.
macros lisp common-lisp
Paul nathan
source share