tags:

views:

155

answers:

1

Is there any way to rename clojure libraries when you load them with require or use?

For example, is there any way to do something like

(require 'some.include.path.some-library :as something-else)

and then reference elements of some-library through something-else/element-name?

Thanks.

+8  A: 

Take a look at http://clojure.org/libs :

A simple lib with embedded explanations:

(ns com.my-company.clojure.examples.my-utils
  (:import java.util.Date)
  (:use [clojure.contrib.def :only (defvar-)])
  (:require [clojure.contrib.shell-out :as shell]))
  • The ns form names the lib's namespace and declares its dependencies. Based on its name, this lib must be contained in a Java resource at the classpath-relative path: com/my_company/clojure/examples/my_utils.clj (note the translations from period to slash and hyphen to underscore).
  • The :import clause declares this lib's use of java.util.Date and makes it available to code in this lib using its unqualified name.
  • The :use clause declares a dependency on the clojure.contrib.def lib for its defvar- function only. defvar- may be used in this lib's code using its unqualified name.
  • The :require clause declares a dependency on the clojure.contrib.shell-out lib and enables using its members using the shorter namespace alias shell.
edbond
Is the crucial difference between `:use` and `:require` here just the aliasing capability of the latter? The documentation for `use` mentions that it's like `require`, but also applies `refer` to the arguments. I have a hard time remembering the differences.
seh
seh: see http://stackoverflow.com/questions/871997/use-vs-require-in-clojure
edbond
Yes, but is there a way to (require [clojure.contrib :as contrib]) and then reference objects in clojure.contrib as, for example, contrib/def instead of clojure.contrib/def?
Nate
Namespaces do not form a hierarchy. You can do `(require [clojure.contrib.def :as d])` and then access eg. `defvar` as `d/defvar`. `clojure.contrib` has no meaning besides being a symbol if there is no namespace of that name. So what you want is not possible.
kotarak