Can anyone explain the difference between use
and require
, both when used directly and as :use
and :require
in the ns
macro?

- 48,199
- 22
- 128
- 192

- 11,414
- 4
- 32
- 37
-
2See also http://stackoverflow.com/questions/10358149/in-clojure-1-4-what-is-the-use-of-refer-within-require with regard to the ns macro; in clojure 1.4 it's suggested you use :require by preference to :use – Korny Apr 28 '13 at 06:22
4 Answers
require
loads libs (that aren't already loaded), use
does the same plus it refers to their namespaces with clojure.core/refer
(so you also get the possibility of using :exclude
etc like with clojure.core/refer
). Both are recommended for use in ns
rather than directly.

- 854,459
- 170
- 1,222
- 1,395
-
3If I require lib foo, then to use bar in foo, I'd have to write foo/bar every time, right? Why would you want to load a lib in ns but then not refer it into the ns? I guess you might be worried about collisions, and you don't want to bother having to reconcile them, right? – Jegschemesch May 16 '09 at 12:14
-
13not having to reconcile collisions is a good point, and more generally there's a programming style which says "namespaces are a honking great idea, we should have more of them" (from "The Zen of Python") -- so e.g. that style recommends not using "using namespace foo;" in C++, so that readers and maintainers of the code won't have to worry "where does this bar come from" but see a more explicit foo::bar instead. require (vs use) supports this "explicit namespaces" style. – Alex Martelli May 18 '09 at 04:20
-
2Alex gives a good but outdated answer. As @overthink points out below, after this answer was given idiomatic clojure recommends require over use. see: http://dev.clojure.org/jira/browse/CLJ-879 – Phil Cooper Mar 08 '16 at 13:27
-
While this is the accepted and most up-voted answer, it is old and represent an outdated view. The better answer is that from @rzv : https://stackoverflow.com/a/16429572/172272 – Didier A. Mar 31 '19 at 05:15
It's idiomatic to include external functions with require
and refer
. You avoid namespace conflicts, you only include functions you actually use/need, and you explicitly declare each function's location:
(ns project.core
(:require [ring.middleware.reload :refer [wrap-reload]]))
I do not have to invoke this function by prefixing it with its namespace:
(wrap-reload) ; works
If you don't use refer
you'll need to prefix it with the namespace:
(ring.middleware.reload/wrap-reload) ; works if you don't use refer in your require
If you choose use
instead, (pretty much) always use only
:
(ns project.core
(:use [ring.middleware.reload :only [wrap-reload]]))
Otherwise you're including everything, making it both an unnecessarily large operation and very confusing for other programmers to find where the functions live.
Also, I highly recommend this blog as a resource for learning more about Clojure namespaces.

- 37,128
- 15
- 99
- 111

- 2,022
- 18
- 22
-
Do you know if there is there any difference in the end between `(:use foo :only [bar])` and `(:require foo :refer [bar])`? Seems odd to have two ways to do this. – overthink Jul 04 '13 at 01:03
-
10Looks like http://stackoverflow.com/a/10370672/69689 answers my question. In short: `(:require .. :refer ..)` is a new way to do the same thing that allows you to effectively deprecate `:use`, which has some drawbacks. – overthink Jul 04 '13 at 01:35
Use sure does make it easier by not requiring you to spell out the namespace every time you want to call a function though it can also make a mess of things by creating namespace conflicts. A good middle ground between "use" and "require" is to only 'use' the functions from a namespace that you actually use.
for instance:
(use '[clojure-contrib.duck-streams :only (writer reader)])or even better, specify it at the top of the file in the namespace definition:
(ns com.me.project (:use [clojure.contrib.test-is :only (deftest is run-tests)]))

- 14,616
- 5
- 46
- 70

- 90,827
- 27
- 201
- 284
-
3Thanks for including (pun) the `(ns ...)` syntax; I had been looking for that but all the examples I found were for plain `(use ...)`. – paul Jan 07 '12 at 19:11
-
1UPDATE: this method has been obsoleted now in favor of `(require '[namepase :refer [var-name1 var-name2]])` – Arthur Ulfeldt Dec 29 '14 at 21:48
-
@ArthurUlfeldt You might want to update your answer to include (pun) this. – bfontaine May 19 '16 at 14:57
As has been mentioned the big difference is that with (require 'foo)
, you then refer to names in the lib's namespace like so: (foo/bar ...)
if you do (use 'foo)
then they are now in your current namespace (whatever that may be and provided there are no conflicts) and you can call them like (bar ...)
.

- 3,000
- 26
- 43