SamLosAngeles
SamLosAngeles

Reputation: 2958

Difference between Use, Require and Import

Can any one give me a good example answer to differentiate between Use, Require and Import.

I hope someone can help me.

Upvotes: 5

Views: 808

Answers (3)

SamLosAngeles
SamLosAngeles

Reputation: 2958

require loads and compiles Clojure namespaces. import allows you to avoid using fully-qualified Java class names (the same as import in Java).

Upvotes: 0

Hendekagon
Hendekagon

Reputation: 4643

In short, use require

You'll almost never want to mix-up symbols from different namespaces in the same namespace the way use does, except during casual REPL work.

Upvotes: 1

noisesmith
noisesmith

Reputation: 20194

require ensures that a Clojure namespace has been compiled and instantiated.

  • optionally updating it from source if provided the :reload key
  • optionally creating aliases if the :as key is provided.
  • optionally modifying the current namespace to include mappings to the required namespace's vars, if the :refer key is provided. The mapping is only visible from inside the requiring namespace, and is not transitive to other namespaces requiring it.

use is identical to require in action, except that the default is to modify the current namespace via the refer function to include all the target namespace's vars as if :refer :all had been provided. It accepts the :exclude, :only, and :rename keys to guide the modification of the current namespace.

import is for adding mappings of Class names to the current namespace, so that the package qualifiers will not need to be used.

Upvotes: 7

Related Questions