ClojureScript split one namespace into multiple files

坚强是说给别人听的谎言 提交于 2021-01-27 04:43:51

问题


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?


回答1:


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.


来源:https://stackoverflow.com/questions/31424990/clojurescript-split-one-namespace-into-multiple-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!