In Clojure 1.4 what is the use of refer within require?

Clojure

Clojure Problem Overview


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]]))

Clojure Solutions


Solution 1 - Clojure

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])...

Solution 2 - Clojure

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 ]]))

Solution 3 - Clojure

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrewView Question on Stackoverflow
Solution 1 - ClojureAlex OttView Answer on Stackoverflow
Solution 2 - ClojureArthur UlfeldtView Answer on Stackoverflow
Solution 3 - ClojureClojurevangelistView Answer on Stackoverflow