On Apr 1, 2009, at 16:28, Kevin Van Horn wrote:
> But
>
> (def (wmd x {:a 1}) 37)
>
> gives me an error: "Second argument to def must be a Symbol". This
> confuses me. Doesn't macro expansion happen entirely before
> evaluation? So before the def special form is evaluated, (wmd x {:a
> 1}) should have already been macro expanded to give a symbol.
Special forms are treated pretty much like macros, except that there
is nothing more fundamental they expand to, instead they are handled
directly by the compiler. But for the purpose of analyzing why your
example doesn't work, let's pretend that def is a macro.
The compiler sees "def" as the first element of a list, and notes
that def is a macro (as we are pretending it is). It thus calls the
def macro with the rest of the list elements as arguments. The first
argument to def is (wmd x {:a 1}), the second argument is 37. As you
can see, the first argument is indeed not a symbol, it is a list of
three elements, wmd, x, and {:a 1}. And that's why def rightly
complains - though I can't tell you why it says "second" instead of
"first" argument.
An important point to note about macros (and special forms) is that
they are expanded from the outside inwards, contrary to expression
evaluation, which starts at the innermost arguments and proceeds
outward. It has to be like that because otherwise a macro couldn't
fully define the syntax of its arguments. A macro might well expect
one of its arguments to be a list with a specific format. If that
argument were macroexpanded first, the outer macro would lose control.
In your example, the wmd macro is thus not expanded at all, UNLESS
the def macro copies it somewhere into its output form, which is then
subjected to another round of macro expansion.
Konrad.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---