将Clojure命名空间拆分为多个文件 [英] Splitting a Clojure namespace over multiple files

查看:125
本文介绍了将Clojure命名空间拆分为多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用:gen-class 进行提前编译时,是否可以将Clojure命名空间分割为多个源文件?如何(:main true)(defn- ...)开始使用?

Is it possible to split a Clojure namespace over multiple source files when doing ahead-of-time compilation with :gen-class? How do (:main true) and (defn- ...) come into play?

推荐答案

概述



当然你可以,事实上 clojure。核心命名空间本身是这样拆分的,并提供了一个很好的模型,你可以通过查找 src / clj / clojure

Overview

Certainly you can, in fact clojure.core namespace itself is split up this way and provides a good model which you can follow by looking in src/clj/clojure:

core.clj
core_deftype.clj
core_print.clj
core_proxy.clj
..etc..

所有这些文件参与构建单个 clojure.core

All these files participate to build up the single clojure.core namespace.

其中之一是主文件,命名为以匹配命名空间名称,以便在有人在:use :require 中提及它时找到它。在这种情况下,主文件是 clojure / core.clj ,它以一个 ns 形式开始。这是您应该将全部命名空间配置,无论您的其他文件可能需要它们。这通常包括:gen-class ,例如:

One of these is the primary file, named to match the namespace name so that it will be found when someone mentions it in a :use or :require. In this case the main file is clojure/core.clj, and it starts with an ns form. This is where you should put all your namespace configuration, regardless of which of your other files may need them. This normally includes :gen-class as well, so something like:

(ns my.lib.of.excellence
  (:use [clojure.java.io :as io :only [reader]])
  (:gen-class :main true))

然后在主文件中的适当位置(最常见的是在末尾)使用 load 以引入您的帮助程序文件。在 clojure.core 它看起来像这样:

Then at appropriate places in your primary file (most commonly all at the end) use load to bring in your helper files. In clojure.core it looks like this:

(load "core_proxy")
(load "core_print")
(load "genclass")
(load "core_deftype")
(load "core/protocols")
(load "gvec")

请注意,您不需要将当前目录作为前缀,需要 .clj 后缀。

Note that you don't need the current directory as a prefix, nor do you need the .clj suffix.

每个帮助文件应该首先声明他们正在帮助的命名空间,但是应该使用 in-ns 函数来这样做。所以对于上面的示例命名空间,帮助文件都将以:

Each of the helper files should start by declaring which namespace they're helping, but should do so using the in-ns function. So for the example namespace above, the helper files would all start with:

(in-ns 'my.lib.of.excellence)

这是所有需要。

因为所有这些文件都在构建单个命名空间,所以你定义的每个函数都可以在任何主文件或帮助文件中。这当然意味着你可以在你想要的任何文件中定义 gen-class 函数:

Because all these files are building a single namespace, each function you define can be in any of the primary or helper files. This of course means you can define your gen-class functions in any file you'd like:

(defn -main [& args]
  ...)

请注意,Clojure的正常排序规则仍然适用于所有函数,因此您需要确保在尝试使用之前加载了某个函数的任何文件

Note that Clojure's normal order-of-definition rules still apply for all functions, so you need to make sure that whatever file defines a function is loaded before you try to use that function.



Private Vars



- foo ...)形式,它定义了一个命名空间私有函数。像这样定义的函数以及其他:private vars是从定义它们的命名空间中可见的,所以主和所有帮助文件都可以访问定义的私有var在到目前为止加载的任何文件中。

You also asked about the (defn- foo ...) form which defines a namespace-private function. Functions defined like this as well as other :private vars are visible from within the namespace where they're defined, so the primary and all helper files will have access to private vars defined in any of the files loaded so far.

这篇关于将Clojure命名空间拆分为多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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