开发者

Clojure namespaces

I want to split a big clojure-script into smaller ones. And it looks like this.

One:

(ns one
  (:use [two :only (show)]))

(def status "WORKING")

Two:

(ns two
  (:use [one :only (status)]))

(defn show [] (println status))

Result: Exception.

PS I understand that some some kind of recursive namespace constructing happens开发者_如何学JAVA. I know only a sloppy half-solution, like defining without body before referncing to namespaces? Any suggestions?


+1 for the answer of ponzao. To elaborate a bit more: Cyclic dependencies of namespaces are often a sign, that you didn't get your abstractions and/or APIs right. Either you "mix" layers or things should just be in one namespace, because the really belong together.

If you want to just split one namespace into several files, this is also possible.

name/space.clj:

(ns name.space)

(declare status)

(load "space_one")
(load "space_two")

name/space_one.clj:

(in-ns 'name.space)
(defn show [] (println status))

name/space_two.clj:

(in-ns 'name.space)
(def status "WORKING")


You are constructing a cyclic dependency between two components, are you sure this is what you want? Why not have a third namespace containing their common functions?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜