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

查看:168
本文介绍了在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. 对于Java包,我是否使用与Clojure命名空间相同的唯一性约定? [ie backwards-company-domain.project.subsystem]

  2. 我应该将我的文件保存在与我的命名空间匹配的目录结构中吗? [ala Java]

  3. 如果我有多个命名空间,我需要将我的所有代码编译成一个jar,并将其添加到我的类路径,使其可访问?

  4. 每个命名空间是否应编译为一个jar?或者我应该创建一个单一的jar包含来自许多命名空间的clj代码?

  1. Do I use the same uniqueness conventions for Clojure namespaces as I would normally use for Java packages? [ie backwards-company-domain.project.subsystem]
  2. Should I save my files in a directory structure that matches my namespaces? [ala Java]
  3. If I have multiple namespaces, do I need to compile all of my code into a jar and add it to my classpath to make it accessible?
  4. Should each namespace compile to one jar? Or should I create a single jar that contains clj code from many namespaces?

感谢...

推荐答案


  1. 我想如果你认为它有帮助,但是很多Clojure项目不会这样做 - 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。在 my 目录中,在 cool_project.clj 文件中定义了cool-project )。

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,文件系统上的jars和目录...只要它遵循正确的命名约定(你的第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.

我想说你应该使用jars作为功能单位码。例如。 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天全站免登陆