Re: More questions about exported symbols and general style.



Pillsy wrote:
Well, two questions about exporting symbols, really. Say I want to set
up bindings to captured variables (like IT in anaphoric macros) in
packages that I'll be using. Do I want to just export IT and friends,
or should I walk through the code and replace them with gensyms or
something?

Also, I've found I've gotten in the habit of naming captured variables
like IT names that begin with dollar signs, i.e., $IT, so they stand
out a bit. Bad, good or indifferent idea?

What you have to keep in mind is that anaphoric macros are only useful as surface syntax. Other macros should not expand into anaphoric macros because otherwise they may cause unintended capture of the special variable names.

Consider AIF being a macro that creates a new implicit binding for IT. Now check the following example:

(aif (some-expression ...)
(some-macro ...
(+ it 5)))

If SOME-MACRO expands into a use of AIF (or some other anaphoric macro introducing a new binding for IT), then the inner (+ it 5) probably doesn't do what you expect it to.

So your design should be based on the intended use of your macros. If your macros are supposed to be used as regular language constructs that other macros can expand into, you should probably better avoid anaphoric macros.

Next, clients of anaphoric macros must be able to see the variable name. This means that you have to export it from your package and they have to import it. Now, IT is a pretty common name for such a purpose, so there is a certain level of likeliness that there will be name clashes. This means that client code probably ends up in having to say SOME-PACKAGE:IT instead of just IT.

One solution, if you insist on using anaphoric macros, is to not consider IT not as a regular variable but just as special syntax. In other words, you would have to parse the macro body and replace all occurences of IT with an internally created variable (via gensym). For example, the LOOP macro does it this way. Futhermore, in LOOP, IT is recognized by its symbol-name (thus you don't have to import/export anything to use it). Maybe that's going a bit too far and a keyword :IT would have been sufficient here.

Anyway, you may simply come to the conclusion that this is not worth the fuzz and just let the client code decide what name to use...


Pascal

--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
.



Relevant Pages