Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences among `require`, `import`, and `use`? [duplicate]

Tags:

clojure

Why do we need all of require, import, and use?

like image 530
z_axis Avatar asked Aug 22 '11 05:08

z_axis


1 Answers

Require

require loads a Clojure library so that you can use it in your current file or REPL.

This is the normal way to access functions and definition in a Clojure library.

Use

use brings in a Clojure namespace in the same way as require, but in addition it refers to the definitions in the loaded namespace from the current namespace (i.e. it creates a convenient alias in the current namespace).

Don't over-use it (pun intended) - it can easily cause namespace conflicts!

Import

import is for importing Java classes and interfaces only.

user=> (import java.util.Date)
java.util.Date

user=> (def *now* (Date.))
#'user/*now*

If you don't need to interoperate with Java code then you can safely ignore import.

like image 61
mikera Avatar answered Oct 21 '22 13:10

mikera