Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Make forward declarations in any namespace to circumvent circular dependency limitations.
(defmacro declare-remote
"Declares the given ns-qualified names, preserving symbol metadata. Useful for
circular dependencies."
[& names]
`(do ~@(map (fn [n]
(let [ns (namespace n)
v (name n)
m (meta n)]
`(do (in-ns '~(symbol ns))
(declare ~(with-meta (symbol v) m))))) names)
(in-ns '~(symbol original-ns)))))

This comment has been minimized.

Copy link
Owner Author

@ptaoussanis ptaoussanis commented Dec 31, 2012

If ns1 depends on ns2/foo and ns2 depends on ns1/bar:

;;;; ns1
(ns proj.ns1 (:require [proj.ns2 :as ns2]))

;;;; ns2
(ns proj.ns2 (:require [proj.ns1 :as ns1])) ; Doesn't work (circular dependency)

Change to:

;;;; ns1
(ns proj.ns1 (:require [proj.ns2 :as ns2]))

;;;; ns2
(ns proj.ns2)
(declare-remote ns1/bar)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.