On Feb 1, 2009, at 12:04 PM, Eric wrote:
So, my question is this: what is the difference/relationship between #' and the fully-qualified name?
#'x is a sequence of characters that the reader (LispReader.java) translates into (var x) when reading. (see http://clojure.org/special_forms#var) .
The simplest way to see this is by quoting it at the repl: user=> (quote #'x) (var x)The purpose of the "var" special form is to return the var object named by its argument (a symbol). It does this by using the symbol's name part as a key in the ns-map for a namespace: (see clojure.core/ns- map)
- If the symbol has a namespace part, var uses the namespace it names; - If not, var uses the current namespace (*ns*).If a symbol has a namespace part, using #' or var is unnecessary because because such a symbol cannot refer to a function argument or let-bound local.
For symbols without a namespace part, by using (var x) or #'x, you're specifying you want the var object associated with that symbol in the ns-map of the current namespace rather than leaving open the possibility of getting the value of a function argument or let-bound local named by that symbol.
When should they be used? What are the standard idioms for using these?
Here are some suggestions of mine. I don't claim they're standard:- Avoid using an argument name or let-bound local name that "shadows" the (unqualified) name of a var you need to use within its scope
- Failing that, use a namespace qualified symbol to refer to the var- If namespace qualifying a symbol is too verbose, use "alias" to create a shorter name for the namespace - If that's still too verbose, use #' or (var ...) to refer to the var in the current namespace - #'x and (var x) are equivalent. Use whichever you find readable and adequately succinct.
Regarding binding itself, binding a var is an operation on the var object itself. For a given var, that operation is the same and has the same effect no matter how you acquired a reference to the var:
- using a fully qualified symbol name- by resolution of a simple symbol name that's not shadowed by a function argument or let-bound local - by resolution of a simple symbol name within a var special form (or #')
- by direct lookup in the ns-map for a namespace Your experiment proved that. --Steve
smime.p7s
Description: S/MIME cryptographic signature