Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
39 lines (32 sloc) 1.3 KB
;;; with_ns.clj -- temporary namespace macro
;; by Stuart Sierra,
;; March 28, 2009
;; Copyright (c) Stuart Sierra, 2009. 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 epl-v10.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.
^{:author "Stuart Sierra",
:doc "Temporary namespace macro"}
(defmacro with-ns
"Evaluates body in another namespace. ns is either a namespace
object or a symbol. This makes it possible to define functions in
namespaces other than the current one."
[ns & body]
`(binding [*ns* (the-ns ~ns)]
~@(map (fn [form] `(eval '~form)) body)))
(defmacro with-temp-ns
"Evaluates body in an anonymous namespace, which is then immediately
removed. The temporary namespace will 'refer' clojure.core."
[& body]
(create-ns 'sym#)
(let [result# (with-ns 'sym#
(finally (remove-ns 'sym#))))
Something went wrong with that request. Please try again.