ClojureDocs

Nav

Namespaces

refer

clojure.core

Available since 1.0 (source)
  • (refer ns-sym & filters)
refers to all public vars of ns, subject to filters.
filters can include at most one each of:
 :exclude list-of-symbols
:only list-of-symbols
:rename map-of-fromsymbol-tosymbol
 For each public interned var in the namespace named by the symbol,
adds a mapping from the name of the var to the var to the current
namespace.  Throws an exception if name is already mapped to
something else in the current namespace. Filters can be used to
select a subset, via inclusion or exclusion, or to provide a mapping
to a symbol different from the var's name, in order to prevent
clashes. Use :use in the ns macro in preference to calling this directly.
3 Examples
user=> (refer 'clojure.string :only '[capitalize trim])
nil

user=> (capitalize (trim " hOnduRAS  "))
"Honduras"
user=> (refer 'clojure.string
              :rename '{capitalize cap, trim trm})
WARNING: replace already refers to: #'clojure.core/replace in namespace: user, being replaced by: #'clojure.string/replace
WARNING: reverse already refers to: #'clojure.core/reverse in namespace: user, being replaced by: #'clojure.string/reverse
nil

user=> (cap (trm " hOnduRAS  "))
"Honduras"

user=> (join \, [1 2 3])
"1,2,3"
;;; `:only' accepts only original names.
;; wrong
user=> (refer 'clojure.string
              :rename '{capitalize cap, trim trm}
              :only '[cap trm])
IllegalAccessError cap does not exist  clojure.core/refer (core.clj:3849)

;; right
user=> (refer 'clojure.string
              :rename '{capitalize cap, trim trm}
              :only '[capitalize trim])
nil

;; work well
user=> (cap (trm " hOnduRAS  "))
"Honduras"

;; and also, cannot use either of them.
user=> (join \, [1 2 3])
CompilerException java.lang.RuntimeException: Unable to resolve symbol: join in this context, compiling:(NO_SOURCE_PATH:1:1)
See Also

Same as (refer 'clojure.core <filters>)

Added by mmwaikar

Sets *ns* to the namespace named by name (unevaluated), creating it if needed. references can be ...

Added by Mark Addleman
1 Note
    By , created 2.3 years ago

    Warning: the referral of a var isn't transitive.

    refer only works with a var's original namespace.

    If you refer A/foo from B, you can't then refer B/foo from C.

    foo it will appear in B's ns-map, but not ns-publics or ns-interns, unintuitively.