Can anyone explain the difference between use
and require
, both when used directly and as :use
and :require
in the ns
macro?
-
2See also stackoverflow.com/questions/10358149/… with regard to the ns macro; in clojure 1.4 it's suggested you use :require by preference to :use– KornyCommented Apr 28, 2013 at 6: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.
-
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? Commented May 16, 2009 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. Commented May 18, 2009 at 4: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: dev.clojure.org/jira/browse/CLJ-879 Commented Mar 8, 2016 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 : stackoverflow.com/a/16429572/172272 Commented Mar 31, 2019 at 5: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.
-
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. Commented Jul 4, 2013 at 1:03 -
10Looks like 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. Commented Jul 4, 2013 at 1: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)]))
-
3Thanks for including (pun) the
(ns ...)
syntax; I had been looking for that but all the examples I found were for plain(use ...)
.– paulCommented Jan 7, 2012 at 19:11 -
1UPDATE: this method has been obsoleted now in favor of
(require '[namepase :refer [var-name1 var-name2]])
Commented Dec 29, 2014 at 21:48 -
@ArthurUlfeldt You might want to update your answer to include (pun) this. Commented May 19, 2016 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 ...)
.