On Jan 20, 2009, at 2:44 PM, samppi wrote:
I'm trying to get into more of how namespaces work. The refer function refers to all the public variables of a library. What is the point of refer-clojure, though? When would you use it? What variables of clojure.core are not normally present in a new namespace?
(Adding perhaps a few interesting tidbits to the fine answers already given...)
----------------------------If you use "in-ns" to create a namespace, the only symbols that are resolvable without explicit namespace qualification are:
- special forms, - in-ns, - ns, - java.lang.*,- Compiler (referring to clojure.lang.Compiler, I'm not sure if this is intentional or not.)
Special forms, in-ns, and ns are handled as special cases. You can see the rest by using "ns-map" on a freshly created namespace:
Clojure user=> (in-ns 'fresh) #<Namespace fresh>fresh=> (clojure.core/doseq [e (clojure.core/ns-map 'fresh)] (clojure.core/prn e))
[ProcessBuilder java.lang.ProcessBuilder] [Enum java.lang.Enum] [SuppressWarnings java.lang.SuppressWarnings] [Throwable java.lang.Throwable] [InterruptedException java.lang.InterruptedException] [...]You can use "refer" to bring in some or all of clojure.core (or any other namespace) into the new namespace by reference.
----------------------------If you use "ns" to create a namespace (as you should in nearly all cases--one exception being ad-hoc creation of a namespace interactively at the repl), all of the above plus all symbols in the clojure.core namespace are resolvable without explicit namespace qualification by default.
The purpose of a ":refer-clojure" clause within an ns form is to enable fine tuning of how much of clojure.core is brought into the new namespace. You can use ":only" or ":exclude" to pull in just what you want, or to leave some (or all) things out. The (only) purpose of refer-clojure is to support the :refer-clojure clause.
----------------------------Note: the doc string for "refer-clojure" is outdated--it needs an update (clojure -> clojure.core).
--Steve
smime.p7s
Description: S/MIME cryptographic signature