I'm in the process of learning Clojure, and I ran across something
that other newbies like me may find useful.  The question I had was
this: how does one get a method name into a macro?  Consider the
following code:

  (defmacro foo [x] `(. Character (isWhitespace ~x)))

Yes, I know that this would be better defined as a function, but it's
a simple example to illustrate the problem.

If I evaluate (macroexpand '(foo \a)) I get

  (. java.lang.Character (user/isWhitespace \a))

which is not what I want -- the expansion of Character to
java.lang.Character is fine, but I want the symbol isWhitespace to
remain as is.  Trying to evaluate (foo \a) gives a "no such var"
exception, as user/isWhitespace is not found.

The solution I found was to keep the symbol isWhitespace out of the
scope of the backquote operator:

  (defmacro foo [x] `(. Character (~'isWhitespace ~x)))

Then (foo \a) expands to (. java.lang.Character (isWhitespace \a)) as
desired.

--~--~---------~--~----~------------~-------~--~----~
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