[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [O] Why does evaluating a piece of Elisp code seemingly not expand a
From: |
Oleh Krehel |
Subject: |
Re: [O] Why does evaluating a piece of Elisp code seemingly not expand a macro? |
Date: |
Fri, 15 Jan 2016 11:57:17 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux) |
Marcin Borkowski <address@hidden> writes:
> Why?
Macro-expand the defun to get:
(defalias 'print-answer
#'(lambda nil
(message
"The answer is %s."
(forty-two))))
`lambda' is a macro that /quotes/ its body. Therefore, the body of
`defun' is not evaluated or expanded when it's defined.
You probably wanted something like this instead:
(macroexpand-all
'(lambda nil
(message
"The answer is %s."
(forty-two))))
;; =>
;; (function
;; (lambda nil
;; (message
;; "The answer is %s."
;; 42)))
Which could be wrapped in a new macro:
(defmacro defun-1 (name arglist &optional docstring &rest body)
(unless (stringp docstring)
(setq body
(if body
(cons docstring body)
docstring))
(setq docstring nil))
(list 'defun name arglist docstring (macroexpand-all body)))
The above seems to work, at least superficially:
(symbol-function
(defun-1 print-answer ()
(message "The answer is %s." (forty-two))))
;; =>
;; (lambda nil
;; (message
;; "The answer is %s."
;; 42))
By the way, it might be more appropriate to ask similar questions on
address@hidden
Oleh
Re: [O] Why does evaluating a piece of Elisp code seemingly not expand a macro?, Samuel W. Flint, 2016/01/15