On Mar 28, 1:59 pm, "ke...@ksvanhorn.com" <kvanh...@ksvanhorn.com>
wrote:
> 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.
There's no need to do that. Prefixes on member names are ignored:
user=> (. java.lang.Character (user/isWhitespace \a))
false
user=> (defmacro foo [x] `(. Character (isWhitespace ~x)))
#'user/foo
user=> (foo \a)
false
I don't see how you are getting that error (old version?).
The use of ~' should be reserved for intentional capture.
If you don't want the unsightliness in your expansions, you can use
the Class/staticMember or .instanceMember forms in syntax quote.
Rich
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---