This repository has been archived by the owner. It is now read-only.
(archived) Clojure 'need' macro to replace use/require/refer
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

need: a macro to load Clojure namespaces

by Stuart Sierra,

Copyright (c) Stuart Sierra, 2010. All rights reserved. The use and distribution terms for this software are covered by the Eclipse Public License 1.0 ( which can be found in the file LICENSE.html at the root of this distribution. By using this software in any fashion, you are agreeing to be bound by the terms of this license. You must not remove this notice, or any other, from this software.

The Need for need

A lot of new Clojure users have trouble remembering the correct syntax of use/require/refer.

The need macro is an attempt to simplify the process of loading Clojure namespaces.

Unfortunately, need is not backwards-compatible with the existing use/require/refer syntax. It would probably be too disruptive to adopt this in Clojure right now, but it's an idea to think about for the future.

I considered having need also replace import for Java classes, but I think that loading Clojure namespaces and importing Java classes are fundamentally different operations and should not be conflated.


To load a namespace without referring any symbols:


To load a namespace and refer two symbols:

(need [ reader writer])

To load a namespace and refer all symbols:

(need [ :all])

To load a namespace and refer all symbols except one:

(need [ :exclude [spit]])

To load a namespace and refer 3 symbols, renaming one of them. The :rename map is like {original-name new-name}.

(need [ reader writer
         :rename {spit put-file}])

To load a namespace and alias it in the current namespace:

(need [ :as io])


need does not support prefix lists because they are too confusing when combined with vectors of symbols and make it hard to do search-and-replace on namespace names.

need* is a function that behaves like need but evaluates its arguments.

need and need* both accept the flags :reload, :reload-all, and :verbose.

See additional documentation in the docstring of the need macro.