Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClojureScript split one namespace into multiple files

I've read this thread, but it seems like there are no load and load-file in ClojureScript. Is it possible to separate a single namespace over multiple files?

The reason I want to do that is because I'm using Om and I want to separate components into different files. I can do it using separate namespaces, but then I will have to write the same requires in the beginning of each file and also the only way to call those components in the main file is like that:

(:require [some-project.sidebar :as sidebar])

...

(om/build sidebar/sidebar app-state)

i.e. I have to specify namespace before each component's name, which doesn't look pretty. Any ideas on how to improve it? I'm new to Clojure and ClojureScript, so maybe I'm missing something obvious?

like image 374
Victor Marchuk Avatar asked Jul 15 '15 08:07

Victor Marchuk


1 Answers

There are a few things to note here

  1. You can use :refer in your :require to import unqualified vars into a namespace. This is ok if there are a few, but can quickly get unwieldy if you tried to do it for everything.
  2. Clojure applications are often structured in a tree like fashion where a main namespace requires sub namespaces, and so on, so you won't necessarily be importing the same namespaces into every namespace.
  3. Even if it was possible to split a namespace across multiple files, it wouldn't be idiomatic Clojure. One file = one namespace is the norm.
  4. If you wanted, you can def vars from one namespace into another to make one 'master' namespace to use in your other namespaces.
  5. If you want to minimise the number of imports you have to do, make fewer namespaces and make them bigger.
like image 145
Daniel Compton Avatar answered Oct 21 '22 16:10

Daniel Compton