5

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

I hope someone can help me.

SamLosAngeles
  • 2,660
  • 2
  • 14
  • 12
  • 2
    another duplicate http://stackoverflow.com/questions/871997/difference-between-use-and-require?rq=1 and http://stackoverflow.com/questions/3408076/difference-in-clojure-between-use-and-require?rq=1 – edbond Sep 22 '14 at 08:02

3 Answers3

7

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.

noisesmith
  • 20,076
  • 2
  • 41
  • 49
  • `The mapping is only visible from inside the requiring namespace, and is not transitive to other namespaces requiring it.` Is `require` otherwise transitive? – Shinto C V Apr 28 '20 at 01:16
1

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.

Hendekagon
  • 4,565
  • 2
  • 28
  • 43
0

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

jpaugh
  • 6,634
  • 4
  • 38
  • 90
SamLosAngeles
  • 2,660
  • 2
  • 14
  • 12