pnathan: elephant bypasses fence to drink from pool (Default)
pnathan ([personal profile] pnathan) wrote2022-12-10 06:23 pm
Entry tags:

yet another defclass wrapping macro

Another entry in the "simplified defclass" reckoning:


(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.

Post a comment in response:

This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting