On Tue, Jan 20, 2009 at 2:59 PM, Stuart Halloway <stuart.hallo...@gmail.com> 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? > > All of them, if you use in-ns: > > (in-ns 'foo) > #<Namespace foo> > > > str > java.lang.Exception: Unable to resolve symbol: str in this context > > (clojure.core/refer-clojure) > -> nil > > str > -> #<core$str__2981 clojure.core$str__2...@bf8aa92>
While this is true, it's not the primary purpose of 'refer-clojure', since you could instead simply do: (clojure.core/refer 'clojure.core) The reason 'refer-clojure' exists is for use with the 'ns' macro. By default, 'ns' refers all of clojure.core, which is usually what you want. However, if you want to restrict or change any of the clojure.core symbols on their way into your new namespace, you can use :refer-clojure and 'ns' will let you do what you want. Let's say I want a new "normal" namespace, except without the builtin 'slurp' function: (ns bar (:refer-clojure :exclude (slurp))) bar=> slurp java.lang.Exception: Unable to resolve symbol: slurp in this context (NO_SOURCE_FILE:0) Why would I want to do something like this? Perhaps because I want to use a different version of 'slurp': (ns bar (:refer-clojure :exclude (slurp)) (:use [clojure.contrib.mmap :only (slurp)])) bar=> str #<core$str__2987 clojure.core$str__2...@1ee148b> bar=> slurp #<mmap$slurp__205 clojure.contrib.mmap$slurp__...@7ecd78> Note that 'ns' is meant to be used in .clj files, not at the REPL. --Chouser --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---