pnathan: elephant bypasses fence to drink from pool (Default)
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.

Syndicate

RSS Atom

Most Popular Tags

Expand Cut Tags

No cut tags
Page generated Jul. 4th, 2025 12:25 pm
Powered by Dreamwidth Studios