I'm trying to implement a macro that creates both a struct and a function. The following code template:
Code: Select all
(defmacro macro-example (...)
`(defstruct ...)
`(defun ....))
Regards,
Luis.
Code: Select all
(defmacro macro-example (...)
`(defstruct ...)
`(defun ....))
Code: Select all
(defmacro macro-example (...)
`(progn
(defstruct ...)
(defun ....)))
Although do note that top level PROGN is specified to preserved top-levelness of forms, while other forms are not. This is usually not critical, but can affect compilation in ways which I never investigated in detail, but it is best avoided.edgar-rft wrote:Any other construct using an implicit PROGN will work, too.
Thank you. I should have come up with this straightforward solution.edgar-rft wrote:Try this:
Any other construct using an implicit PROGN will work, too.Code: Select all
(defmacro macro-example (...) `(progn (defstruct ...) (defun ....)))
- edgar
It's correct that PROGN is only guarateed to evaluate forms at the top-level if called at the top-level, so it's not guaranteed 100% for sure that code like this will work:Edgar: Any other construct using an implicit PROGN will work, too.
Ramarren: Although do note that top level PROGN is specified to preserved top-levelness of forms, while other forms are not. This is usually not critical, but can affect compilation in ways which I never investigated in detail, but it is best avoided.
Code: Select all
(defmacro macro-example (...)
;; some heavily nested Lisp forms, where
;; PROGN is called deep inside the nesting
`(foo ...
(bar ...
(baz ...
(progn
(defstruct ...)
(defun ....))))))
Code: Select all
CL-USER> (defmacro macro-example ()
`(let ((x 1))
(defun add-x (arg) (+ arg x))))
MACRO-EXAMPLE
CL-USER> (macro-example)
ADD-X
CL-USER> (add-x 1)
2