Function: macroexp-let2
macroexp-let2 is a macro defined in macroexp.el.gz.
Signature
(macroexp-let2 TEST SYM EXP &rest BODY)
Documentation
Evaluate BODY with SYM bound to an expression for EXP's value.
The intended usage is that BODY generates an expression that will refer to EXP's value multiple times, but will evaluate EXP only once. As BODY generates that expression, it should use SYM to stand for the value of EXP.
If EXP is a simple, safe expression, then SYM's value is EXP itself. Otherwise, SYM's value is a symbol which holds the value produced by evaluating EXP. The return value incorporates the value of BODY, plus additional code to evaluate EXP once and save the result so SYM can refer to it.
If BODY consists of multiple forms, they are all evaluated but only the last one's value matters.
TEST is a predicate to determine whether EXP qualifies as simple and safe; if TEST is nil, only constant expressions qualify.
Example:
(macroexp-let2 nil foo EXP
`(* ,foo ,foo))
generates an expression that evaluates EXP once,
then returns the square of that value.
You could do this with
(let ((foovar EXP))
(* foovar foovar))
but using macroexp-let2 produces more efficient code in
cases where EXP is a constant.
Source Code
;; Defined in /usr/src/emacs/lisp/emacs-lisp/macroexp.el.gz
(defmacro macroexp-let2 (test sym exp &rest body)
"Evaluate BODY with SYM bound to an expression for EXP's value.
The intended usage is that BODY generates an expression that
will refer to EXP's value multiple times, but will evaluate
EXP only once. As BODY generates that expression, it should
use SYM to stand for the value of EXP.
If EXP is a simple, safe expression, then SYM's value is EXP itself.
Otherwise, SYM's value is a symbol which holds the value produced by
evaluating EXP. The return value incorporates the value of BODY, plus
additional code to evaluate EXP once and save the result so SYM can
refer to it.
If BODY consists of multiple forms, they are all evaluated
but only the last one's value matters.
TEST is a predicate to determine whether EXP qualifies as simple and
safe; if TEST is nil, only constant expressions qualify.
Example:
(macroexp-let2 nil foo EXP
\\=`(* ,foo ,foo))
generates an expression that evaluates EXP once,
then returns the square of that value.
You could do this with
(let ((foovar EXP))
(* foovar foovar))
but using `macroexp-let2' produces more efficient code in
cases where EXP is a constant."
(declare (indent 3) (debug (sexp sexp form body)))
(let ((bodysym (make-symbol "body"))
(expsym (make-symbol "exp")))
`(let* ((,expsym ,exp)
(,sym (if (funcall #',(or test #'macroexp-const-p) ,expsym)
,expsym (make-symbol ,(symbol-name sym))))
(,bodysym ,(macroexp-progn body)))
(if (eq ,sym ,expsym) ,bodysym
(macroexp-let* (list (list ,sym ,expsym))
,bodysym)))))