Clojure自动需要文件 [英] Clojure automatically require files

查看:113
本文介绍了Clojure自动需要文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在clojure中创建小的Web框架。我有一堆clojure文件在/ handlers目录,我想要在我的项目的.core命名空间中的所有他们。每个文件定义自己的命名空间。例如:project.handlers.home。这个想法是当我添加新的处理程序,我不想修改在我的核心文件中的命名空间声明包括它。我唯一的解决方案是找到目录中的所有文件,并加载它们与clojure.core /加载,但它是远离美丽和惯用。

I am trying to make little web framework in clojure. I have a bunch of clojure files in /handlers directory and I want to require all of them in my project's .core namespace. Every file defines its own namespace. For example: project.handlers.home. The idea behind this is when I add new handler, I don't want to modify namespace declaration in my core file to include it. The only solution I came up is to find all files in directory and load them with clojure.core/load, but it is far from beautiful and idiomatic. Is there a idiomatic way to do this?

推荐答案


有没有惯用的方法来做到这一点?

Is there a idiomatic way to do this?

IMO,no。 Clojure中的成语通常倾向于显式地做魔术,特别是在命名全局对象时(自动加载命名空间很清楚)。

IMO, no. The idioms in Clojure usually favor being explicit over "doing magic", especially when it comes to naming global objects (which auto-loading namespaces clearly is).

知道为什么你不想在添加新的处理程序时修改你的核心文件,但你可以考虑引入一个额外的命名空间,加载核心和你的处理程序,并将它们钩在一起。

I don't know why you wouldn't want to modify your "core" file when adding new handlers but you might consider introducing an additional namespace that loads both "core" and your handlers and hooks them together.

这篇关于Clojure自动需要文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆