Clojure automatically requires files

I am trying to create a small web structure in clojure. I have a bunch of clojure files in / handlers directory and I want them all to be contained in the .core namespace. Each file defines its own namespace. For example: project.handlers.home. The idea behind this is that when I add a new handler, I don't want to change the namespace declaration in my main file to include it. The only solution I chose was to find all the files in the directory and load them using clojure.core / load, but this is far from pretty and idiomatic. Is there an idiomatic way to do this?

+2


source to share


2 answers


Is there an idiomatic way to do this?

IMO, no. Idioms in Clojure usually prefer to be explicit about "magic", especially when it comes to naming global objects (which obviously take up autoloaded space).



I don't know why you don't want to change your "main" file when adding new handlers, but you might consider introducing an additional namespace that loads the "kernel" and your handlers and concatenates them together.

+7


source


Noir has included some functionality like this, making it an explicit API call to load namespaces in a specific directory. See load-views for an example . He used this to load paths automatically.

However, Noir's approach did not feel idiomatic due to the amount of magic involved, as well as the additional complications from the approach (such as drawn out path definitions).



If you need to find namespaces from a toolkit, framework or library, I would use find-namespaces

intools.namespace

and then require / load them. This approach can be useful in terms of providing user-level connectivity where the user can drop the handler into the directory and then see the new options in the code, although again the explicit tends to be significantly cleaner.

+1


source







All Articles