73

What advantage does using :refer in :require have over using :only in :use? Are the following synonymous?

(ns so.example (:use [my.lib :only [function]]))

and

(ns so.example (:require [my.lib :refer [function]]))
Andrew
  • 7,286
  • 3
  • 28
  • 38

3 Answers3

84

Main idea of adding :refer to :require is to get rid completely of :use, leaving only one operator to load other packages. You can emulate existing :use with (:require [my.lib :refer :all])...

Alex Ott
  • 80,552
  • 8
  • 87
  • 132
  • 1
    I understand that `:refer` allows us to get rid of `:use`, but can you point me to documentation that indicates that this was the idea? I personally think that `:use` is convenient in certain cases, such as with `clojure.test`. Why not use `:use` in such cases? – Eric Wilson Nov 20 '12 at 19:30
  • 10
    see http://dev.clojure.org/jira/browse/CLJ-879 & link to the discussion in clojure-dev mailing list... – Alex Ott Nov 20 '12 at 20:30
26

yes, they are equivalent,

:refer and :require are the basic operations required to build namespaces. :use is more convienient

  • :require causes classes to be loaded
  • :refer adds things to the name space which is really just a map (actually a couple of maps)
  • :use is :refer + :require

as much is it may look like it, there really is no magic to namespaces

if you make a namespace like this

(ns so.example (:use my.lib))

the equivalent with :require would be:

(ns so.example (:require [my.lib :refer [function1 function2 function3 
                                         list every function in example 
                                         here and remember to keep it 
                                         up to date ]]))
Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284
  • 1
    its almost alwasy used with :as to give short names to packages. ohh and it came first. use calls :refer, refer does not use :use – Arthur Ulfeldt Apr 27 '12 at 21:38
  • I still don't understand why `:refer` was added to `:require`. What problem has been solved that wasn't already? – Andrew Apr 27 '12 at 21:52
  • 16
    Andrew, main idea of adding `:refer` to `:require` is to get rid completely from `:use`. Now you can emulate `:use` with `(:require [my.lib :refer :all])`... – Alex Ott Apr 28 '12 at 07:42
  • @AlexOtt Thanks. If you put your comment as an answer I'll accept it. – Andrew Apr 28 '12 at 09:25
1

As of the 1.4.0 release, there's no longer a good reason to use use. Use require :refer instead. From the Clojure 1.4.0 changelog: "require can now take a :refer option. :refer takes a list of symbols to refer from the namespace or :all to bring in all public vars." (from https://8thlight.com/blog/colin-jones/2010/12/05/clojure-libs-and-namespaces-require-use-import-and-ns.html)

Clojurevangelist
  • 564
  • 1
  • 8
  • 13