Skip to content

Instantly share code, notes, and snippets.

@lispm
Created June 22, 2017 22:39
Show Gist options
  • Save lispm/6ed292af4118077b140df5d1012ca646 to your computer and use it in GitHub Desktop.
Save lispm/6ed292af4118077b140df5d1012ca646 to your computer and use it in GitHub Desktop.
(defun psymb (package &rest args)
(values (intern (apply #'mkstr args) package)))
(defmacro with-struct ((name . fields) struct &body body)
(let ((gs (gensym)))
`(let ((,gs ,struct))
(let ,(mapcar #'(lambda (f)
`(,f (,(psymb (symbol-package name) name f) ,gs)))
fields)
,@body))))
#|
Example:
CL-USER 20 > (pprint (macroexpand-1 '(with-struct (foo- a b c) s (list a b c))))
(LET ((#:G7640 S))
(LET ((A (FOO-A #:G7640))
(B (FOO-B #:G7640))
(C (FOO-C #:G7640)))
(LIST A B C)))
You can now tell it which package to use:
CL-USER 21 > (pprint (macroexpand-1 '(with-struct (color::foo- a b c) s (list a b c))))
(LET ((#:G7641 S))
(LET ((A (COLOR::FOO-A #:G7641))
(B (COLOR::FOO-B #:G7641))
(C (COLOR::FOO-C #:G7641)))
(LIST A B C)))
|#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment