在 Clojure 中使用命名空间的常见约定是什么? [英] What are common conventions for using namespaces in Clojure?

查看:35
本文介绍了在 Clojure 中使用命名空间的常见约定是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难找到在 Clojure 中使用命名空间的好的建议和通用实践.我意识到命名空间与 Java 包不同,所以我试图梳理 Clojure 中的约定,这似乎很难确定.

I'm having trouble finding good advice and common practices for the use of namespaces in Clojure. I realize that namespaces are not the same as Java packages so I'm trying to tease out the conventions in Clojure, which seem surprisingly hard to determine.

我想我很清楚如何将函数拆分为 clj 文件,甚至大致了解如何将这些文件组织到目录中.但除此之外,我很难找到适合我的开发环境的机制.一些相互关联的问题:

I think I have a pretty good idea how to split functions into clj files and even roughly how I'd want to organize those files into directories. But beyond that I'm having trouble finding the mechanics for my dev environment. Some inter-related questions:

  1. 我是否对 Clojure 命名空间使用了与通常用于 Java 包相同的唯一性约定?[即向后公司域.project.subsystem]
  2. 我应该将文件保存在与命名空间匹配的目录结构中吗?[阿拉爪哇]
  3. 如果我有多个命名空间,是否需要将我的所有代码编译成一个 jar 并将其添加到我的类路径中以使其可访问?
  4. 是否应该将每个命名空间编译为一个 jar?或者我应该创建一个包含来自许多命名空间的 clj 代码的单个 jar?

谢谢...

推荐答案

  1. 如果您认为它有帮助,我想没关系,但是许多 Clo​​jure 项目并没有这样做——参见.Compojure(使用顶级 compojure ns 和各种 compojure.* ns 用于特定功能)、Ring、Leiningen... Clojure 本身使用 clojure.*(和 clojure.contrib.* 用于 contrib 库),但这是一个特例,我想.

  1. I guess it's ok if you think it helps, but many Clojure projects don't do so -- cf. Compojure (using a top-level compojure ns and various compojure.* ns's for specific functionality), Ring, Leiningen... Clojure itself uses clojure.* (and clojure.contrib.* for contrib libraries), but that's a special case, I suppose.

是的!您绝对必须这样做,否则 Clojure 将无法找到您的名称空间.另请注意,不得在命名空间名称中使用下划线或在文件名中使用连字符,并且无论在何处使用命名空间名称中的连字符,都必须在文件名中使用下划线(以便 ns my.cool-project 在名为 my 的目录中名为 cool_project.clj 的文件中定义.

Yes! You absolutely must do so, or else Clojure won't be able to find your namespaces. Also note that you musn't use the underscore in namespace names or the hyphen in filenames and wherever you use a hyphen in a namespace name, you must use an underscore in the filename (so that the ns my.cool-project is defined in a file called cool_project.clj in a directory called my).

你需要确保你所有的东西都在类路径上,但它是否在一个 jar、多个 jar、文件系统上的 jar 和目录的混合中都无关紧要......只要它遵守正确的命名约定(你的第 2 点)你应该没问题.

You need to make sure all your stuff is on the classpath, but it doesn't matter if it's in a jar, multiple jars, a mixture of jars and directories on the filesystem... As long as it obeys the correct naming conventions (your point no. 2) you should be fine.

但是,如果没有特别的理由,不要提前编译——这可能会阻止您的代码在不同版本的 Clojure 之间移植,而且除了加载时间略有改善.

However, do not compile things ahead-of-time if there's no particular reason to do so -- this may prevent your code from being portable across various versions of Clojure without providing any benefits besides a slightly improved loading time.

有时您仍然需要使用 AOT 编译,特别是在某些 Java 互操作场景中——相关函数/宏的文档总是提到这一点.clojure.contrib 中有一些需要 AOT 的例子;我从来不需要它,所以我不能提供太多细节.

You'll still need to use AOT compilation sometimes, notably in some Java interop scenarios -- the documentation of the relevant functions / macros always mentions that. There are examples of things requiring AOT in clojure.contrib; I've never needed it, so I can't provide much in the way of details.

我认为您应该将 jar 用于代码的功能单元.例如.Compojure 和 Ring 被打包为包含许多命名空间的单个 jar,这些命名空间共同构成了整个包.此外,clojure.contrib 被打包成一个包含多个不相关库的单个 jar;但这又可能是一个特例.

I'd say you should use jars for functional units of code. E.g. Compojure and Ring get packaged as single jars containing many namespaces which together compose the whole package. Also, clojure.contrib is notably packaged as a single jar with multiple unrelated libraries; but that again may be a special case.

另一方面,包含所有项目代码及其依赖项的单个 jar 有时可能对部署有用.查看 Leiningen 构建工具 及其uberjar"工具,如果您认为这类东西可能对你.

On the other hand, a single jar containing all of your project's code together with its dependencies might occasionally be useful for deployment. Check out the Leiningen build tool and its 'uberjar' facility if you think that sort of thing may be useful to you.

这篇关于在 Clojure 中使用命名空间的常见约定是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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