15

My understanding is that it is recommended now to use require :refer instead of use in the ns macro. For example, do:

(ns example.core 
  (:require [clj-json.core :refer [parse-string]]))

instead of

(ns example.core 
  (:use [clj-json.core :only [parse-string]]))

What is the recommended way to deal with :rename which use supports? Specifically, let's say I want to require clojure.data.zip and rename the ancestors and descendants functions which conflict with clojure.core.

In other words I'd like to know the require equivalent for

(:use 
  [clojure.data.zip :rename {ancestors xml-ancestors, 
                             descendants xml-descendants})

4 Answers 4

18
(ns foo
  (:require [clojure.data.zip :refer [ancestors descendants] :rename {ancestors xml-ancestors descendants xml-descendants}]))
13

You :require in one step, and then :refer with :rename in the next.

(ns foo
  (:require clojure.data.zip)
  (:refer [clojure.data.zip :rename {ancestors xml-ancestors,
                                     descendants xml-descendants})

:use has always been a shorthand for :require+:refer, and now the :refer option to :require is a shorthand for the simplest kind of refer.

1
4

Disclaimer: I do not know the "recommended" way to do this. I only know how I would do it. My solution may not be idiomatic Clojure, and I would be surprised if nobody came along with a better answer.


Here's what I would do: :require the package and alias it using :as:

(ns some.big.name.space
  (:require [clojure.data.zip  :as  cdz])
  ... some more imports, maybe ...)

Then the symbols can be accessed using the specified prefix, and don't conflict with any symbols in my some.big.name.space namespace:

(def some-list [cdz/ancestors cdz/descendants ancestors descendants])

If the alias is short, it hardly bothers me to type it, and I feel that my code is clearer -- the cdz/ is a nice visual cue that the symbol is an import.

I know that this doesn't really answer your exact question -- how to use :rename with :require -- but I feel that it's worth mentioning because it avoids polluting my namespaces and I don't have to mess with Clojure's symbol resolution mechanisms.

2
  • Thanks for response. You are making a very good points. I am aware of this approach, just was curious how I do rename if I want to
    – apolenur
    Jan 30, 2013 at 19:37
  • @apolenur yes, sorry my answer wasn't more helpful. Jan 30, 2013 at 19:56
0

If you need to rename functions or macros from Clojure core use this:

(ns foo
  "Contains foo functionality."
  (:refer-clojure :rename {map core-map}))

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.