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

前端 未结 3 1646
误落风尘
误落风尘 2020-12-07 14:39

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



        
相关标签:
3条回答
  • 2020-12-07 14:57

    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)

    0 讨论(0)
  • 2020-12-07 15:18

    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 ]]))
    
    0 讨论(0)
  • 2020-12-07 15:21

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

    0 讨论(0)
提交回复
热议问题