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

Most Popular Tags

Expand Cut Tags

No cut tags
Page generated Jun. 25th, 2025 10:36 am
Powered by Dreamwidth Studios