Subject: Re: Macros in Common Lisp, Scheme, and other languages
From: Erik Naggum <erik@naggum.no>
Date: 07 Sep 2002 04:29:29 +0000
Newsgroups: comp.lang.lisp,comp.lang.scheme,comp.lang.functional
Message-ID: <3240361769031272@naggum.no>

* pcm@cpugrid.net (P.M.)
| Can you explain what compiler-macros are and how they differ from
| ordinary macros?

  There are two important differences.  The first is that they are defined in
  addition to the function, which remains available for normal calls.  The
  second is that they /may/ rewrite a function call at compile-time to
  redirect the call to a different (such as a more type-specific) function or
  to inline the operation with system- or implementation-specific forms.  The
  significance of the /may/ is that a compiler macro may decline to rewrite
  the form, in which case the original function call remains.

  An ordinary macro is, as you may know, always expanded and it is impossible
  to call it indirectly.  The compiler-macro combines the power of rewriting
  with an actual function.  A compiler-macro is more useful in the presence of
  environment information, such that it can know the declared or inferred type
  of variable bindings and act on such knowledge.  One particularly nice way
  to use compiler macros is in combination with keyword arguments; because
  they can be expensive to use in functions that need only a subset of the
  full functionality, a compiler macro can detect the actual keywords used and
  call the applicable specialized function.  E.g., the general function `member´
  may have specialized functions according as the `:key´ or `:test´ arguments
  reflect common usages, like `:key #'car´ or `:test #'string=´.

| Compile-time computation sounds a bit like template metaprogramming in C++.

  Template meta-programming is compile-time computation, but I am not quite
  sure how templates work.  I have not programmed in C++ for about a decade,
  and it was much less involved then.

-- 
Erik Naggum, Oslo, Norway

Act from reason, and failure makes you rethink and study harder.
Act from faith, and failure makes you blame someone and push harder.