changeset 38163:79e758f8571a

(Wrong Time): New node.
author Richard M. Stallman <rms@gnu.org>
date Sat, 23 Jun 2001 16:11:23 +0000
parents 40606e2d6b31
children ec15967a5cf7
files lispref/macros.texi
diffstat 1 files changed, 32 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/lispref/macros.texi	Sat Jun 23 16:10:25 2001 +0000
+++ b/lispref/macros.texi	Sat Jun 23 16:11:23 2001 +0000
@@ -320,6 +320,7 @@
 trouble, and rules to follow to avoid trouble.
 
 @menu
+* Wrong Time::             Do the work in the expansion, not in the macro.
 * Argument Evaluation::    The expansion should evaluate each macro arg once.
 * Surprising Local Vars::  Local variable bindings in the expansion
                               require special care.
@@ -327,6 +328,37 @@
 * Repeated Expansion::     Avoid depending on how many times expansion is done.
 @end menu
 
+@node Wrong Time
+@subsection Wrong Time
+
+  The most common problem in writing macros is doing too some of the
+real work prematurely---while expanding the macro, rather than in the
+expansion itself.  For instance, one real package had this nmacro
+definition:
+
+@example
+(defmacro my-set-buffer-multibyte (arg)
+  (if (fboundp 'set-buffer-multibyte)
+      (set-buffer-multibyte arg)))
+@end example
+
+With this erroneous macro definition, the program worked fine when
+interpreted but failed when compiled.  This macro definition called
+@code{set-buffer-multibyte} during compilation, which was wrong, and
+then did nothing when the compiled package was run.  The definition
+that the programmer really wanted was this:
+
+@example
+(defmacro my-set-buffer-multibyte (arg)
+  (if (fboundp 'set-buffer-multibyte)
+      `(set-buffer-multibyte ,arg)))
+@end example
+
+@noindent
+This macro expands, if appropriate, into a call to
+@code{set-buffer-multibyte} that will be executed when the compiled
+program is actually run.
+
 @node Argument Evaluation
 @subsection Evaluating Macro Arguments Repeatedly