On Mon, 2 Aug 2010 07:23:12 -0400
Andrew Boekhoff <boekho...@gmail.com> wrote:
>
> On Sunday 01 August 2010 21:34:16 Kyle Schaffrick wrote:
>
> > Hello,
> > 
> > I'm trying to write a library with two main parts. The first is a
> > macro, I'll call it 'with-feature, that walks through forms passed
> > inside it, and any time it sees a call to another function in my
> > library, 'feature, do some transformations.
> > 
> > The problem I'm concerned about is as follows: When my macro sees
> > the forms that are passed into it, the symbols come in however the
> > consumer of the library wrote them. So say I :require my library and
> > alias it to 'mylib', then the call 'with-feature is looking for
> > appears as 'mylib/feature. Or, I could :use the library, and then it
> > would appear as 'feature, or I could alias 'feature to 'banana--you
> > get the idea.
> > 
> > I don't want to reserve some magic symbol name that defies namespace
> > rules, so that if the library consumer code uses the symbol 'feature
> > to mean something different, they get bizarre results.
> > 
> > What is a good pattern for writing the "matching" logic in such a
> > selectively-transforming macro so that it can properly find the
> > magic call it's looking for in the presence of normal namespacing
> > behavior?
> > 
> > Thanks,
> > 
> > -Kyle
> 
> 
> Hi,
>   The following technique seems to work for finding out if you've
> been aliased:
> 
> (ns somewhere.trial)
> 
> (let [here *ns*]
>   (defmacro whats-my-name []
>      (some (fn [[k v]] (when (= here v) `(quote ~k)))
>                 (ns-aliases *ns*))))
> 
> user> (require '[somewhere.trial :as aliased]) => 
> user> (aliased/whats-my-name)                     => aliased
> 
> So at the top of with-feature the parser could check for an alias and
> construct the appropriate predicate from the result.
> 
> -Andy
> 

I originally had something similar to this actually, but it doesn't work
for the case when the library is referred in the consumer code with :use
because the symbol's namespace is then nil, and you still can't tell if
they're shadowing it with a let binding, or have renamed it with :as.

However, this idea occurred to me yesterday. What if I check to see if
the symbol is pointing to the *var* containing my magic function,
instead of trying to examine the symbol itself? e.g.:

  (= (resolve symbol-i-am-scanning-in-my-macro)
     #'function-i-am-looking-for)

It seems to work, since at macro-expand time *ns* is bound to the
consumer code's namespace. Does this seem like a reasonable way to deal
with this?

-Kyle

-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
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