title: defmacro with defclass
body: <p>I have a class in Common Lisp:</p>
  
  <pre><code>(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"))
  </code></pre>
  
  <p>I wanted to create a macro that would simplify this redundancy of typing </p>
  
  <pre><code>(defmacro make-slot (slot-name)
    `(slot-name 
       :initarg :,slot-name
       :accessor :,slot-name
       :initform (error "Must supply value")))
  </code></pre>
  
  <p>Eventually I'd like to have (defclass my-cool-class () (make-slots '(foo bar baz)) and get foo, bar, and baz out as slots automagically.</p>
  
  <p>But, when I went to do a <code>macroexpand-1</code> of make-slot, boy howdy did I get reader errors.</p>
  
  <p>The first one was "illegal terminating character after a colon..." and then it kept going. </p>
  
  <p>SBCL 1.0.37.</p>
  
  <p>edit: the examples are syntactically correct on the system, I did some redaction before I copied.</p>
  
  <hr>
  
  <p>Six months later - </p>
  
  <pre><code>(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 &amp;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))):
  </code></pre>
  
  <hr>
  
  <p>Two and a half years later. </p>
  
  <p>I discovered the six-month-old code in the wild elsewhere. While I'm flattered, it also reminds me to update this. </p>
  
  <p>If you like this idea, I keep this code gardened at: <a href="https://github.com/pnathan/defobject" rel="nofollow">https://github.com/pnathan/defobject</a> . As before, its goal is to produce CLOS classes with the minimum of repetitive typing.  A similar system exists called DEFCLASS-STAR. Interested parties are advised to review both.</p>