15 May 2010

Namespaces in Clojure

The only succinct documentation of namespaces I found on Clojure is here.  To quote Brian there:
:refer shouldn't be used by you at all.

:import brings in java classes

:use brings in the names from another clojure namespace without requiring namespace qualifications

:require brings in the names from another clojure namespace requiring namespace qualifications.

So if ns foo has a var bar, if you do (use 'foo), you can just refer to bar, but if you do (require 'foo), you have to say foo/bar, not just bar.
I should add: make sure file names do not include the "-" character even if the namespace you define with (ns ...) has a "-" character in it; replace it with a "_" instead in the file name (the namespace can continue to use the "-" though).

(edit: I will write more about namespaces, especially the syntax of the (ns ...) later.)

No comments: