Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why some native clojure namespaces have to be required and some not

Tags:

clojure

Simple as the headline suggest:

Why do I have to

(require 'clojure.edn)

in order to use e.g.:

(clojure.edn/read-string "9")

And why can I immediately invoke:

(clojure.string/join (range 4) "-")
like image 851
Anton Harald Avatar asked Mar 24 '16 20:03

Anton Harald


People also ask

What is a Clojure namespace?

In ClojureScript, the unit of modularity is the namespace. A namespace is simply a logical grouping of functions and data that can be required and used by another namespace.

What does Gen class do in Clojure?

The generated class automatically defines all of the non-private methods of its superclasses/interfaces. This parameter can be used to specify the signatures of additional methods of the generated class.


2 Answers

Clojure programs start at the top of the "main" namespace (often project-name.core) and evaluate each form from top to bottom. This happens when the program starts, and before any "main" functions are invoked.

When a require expression is evaluated it jumps over to that namespace and does the same thing there. If requires are encountered there it recurses down branches of those namespaces, recursively loading each namespace as required.

So If you don't explicitly state that your namespace requires another namespace, then you are at the mercy of the order that other namspaces you require load their dependencies. Sometimes it will work, and sometimes unrelated changes to the evaulation order of your distant dependencies will break your code.

So please, please, ... declare your own requirements!

like image 119
Arthur Ulfeldt Avatar answered Nov 15 '22 11:11

Arthur Ulfeldt


in the repl (just starting clojure) I have the following ns loaded by default

user=> (pprint (map #(.getName %) (all-ns)))
(clojure.edn
 clojure.core.server
 clojure.java.io
 clojure.java.shell
 clojure.core.protocols
 clojure.string
 clojure.java.browse
 clojure.pprint
 clojure.uuid
 clojure.core
 clojure.main
 user
 clojure.java.javadoc
 clojure.repl
 clojure.walk
 clojure.instant)

in whichever namespace you are in, clojure.edn seems not to be loaded

like image 22
birdspider Avatar answered Nov 15 '22 11:11

birdspider