Does this clarify the point I was making?

When writing macros, you cannot dynamically build one of the syntactic  
sugar forms. For example, you cannot write a macro that expands cls  
and member into (cls/member):

       (defmacro call-static [cls member] `(~cls/~member))
       -> java.lang.Exception: Invalid token: cls

Instead, you should build normal, unsugared forms:

       (defmacro call-static [cls member] `(. ~cls ~member))
       -> nil

>
> On Tue, Feb 3, 2009 at 11:26 AM, Mark Volkmann
> <r.mark.volkm...@gmail.com> wrote:
>>
>> Now I remember what I was thinking about. This isn't so much a
>> difference between macros and functions as it is a rule about
>> something you cannot do in a macro. Quoting from "Programming  
>> Clojure"
>> ...
>>
>> "You cannot write a macro that expands to any of the syntactic sugar
>> forms ... For example, you cannot write a macro that
>> expands to (Math/PI)."
>
> Hm...
>
> (defmacro pi [] 'Math/PI)  ==> #'user/pi
> (macroexpand '(pi))  ==> Math/PI
> (pi)  ==> 3.141592653589793
>
> (defmacro strlen [s] `(.length ~s))  ==> #'user/strlen
> (strlen "hello")  ==> 5
>
> (defmacro mydoc [s] `(:doc ^#'~s))  ==> #'user/mydoc
> (macroexpand '(mydoc doc))  ==> (:doc (clojure.core/meta (var doc)))
> (mydoc doc)  ==> "Prints documentation for a var or special form  
> given its name"
>
> That's a whole lot of sugar that all seems to work ok.  Is there some
> other syntactic sugar form that does not work?  Perhaps he's referring
> to actually producing reader macro usages, like this:
>
> (defmacro mydoc2 [s] `(:doc ~(symbol (str "^#'" s))))  ==> #'user/ 
> mydoc2
> (macroexpand '(mydoc2 doc))  ==> (:doc ^#'doc)
> (mydoc2 doc)  ==> java.lang.Exception: Unable to resolve symbol:  
> ^#'doc
>
> --Chouser
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to