3

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. I know only a sloppy half-solution, like defining without body before referncing to namespaces? Any suggestions?

2 Answers 2

12

+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")
Sign up to request clarification or add additional context in comments.

2 Comments

Clojure does not support cyclic dependencies between namespaces. You have to design to avoid it.
@stuart-sierra Did I say something different?
6

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?

1 Comment

I don't want to over-engineer a simple project by making to many layers of abstractions and etc. One file keeps data, settings, and everything for processing. I wan't to use it as part of web-service. But for desktop-debugging i use simple visualization, which reads settings from main script, but gives a single function to show this thing on a screen. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.