什么是 Kotlin 模块?

What is a Kotlin module?

在 Kotlin 文档中,他们提到了某种 模块 ,例如 documentation 中的 internal 修饰符。

但是我找不到术语 module 本身的任何定义。那么module是什么意思呢?

A module is a set of Kotlin sources compiled together:

  • an IntelliJ IDEA module;
  • a Maven project;
  • a Gradle source set;
  • a set of files compiled with one invocation of the Ant task.

这与可见性修饰符相同docs article。 :)

来自Kotlin的documentation,模块是一组编译在一起的Kotlin文件:

  • 一个 IntelliJ IDEA 模块
  • 一个 Maven 项目;
  • 一个Gradle源集
  • 通过一次调用 <kotlinc> Ant 任务编译的一组文件。

那是@hotkey的,但我想补充一下这个答案。

According to Andrey Breslav,Kotlin 首席语言设计师:

a Kotlin module maps one-to-one to IntelliJ's module (iml-file).

根据 IntelliJ 的 documentation:

Modules allow you to combine several technologies and frameworks in one application. In IntelliJ IDEA, you can create several modules for a project and each of them can be responsible for its own framework.

说到Maven项目或者命令行编译,Andrey states:

Each compiler run, by default, is a separate module: all the binary dependencies will be treated as being not in the module being compiled at the moment.

此外,Gradle 源集是一个模块,但 test 源集可以访问 main 的内部声明。

这意味着如果您的 Gradle 配置中有不同的构建风格导致不同的源集,例如对于生产和调试版本,那么来自一个源集的内部 class 不会可以在另一个源集中使用。

根据 Kotlin language specification §10.2 Modules:

A module is a concept on the boundary between the code itself and the resulting application, thus it depends on and influences both of them. A Kotlin module is a set of Kotlin files which are considered to be interdependent and must be handled together during compilation.

In a simple case, a module is a set of files compiled at the same time in a given project.

  • A set of files being compiled with a single Kotlin compiler invocation
  • A Maven module
  • A Gradle project

In a more complicated case involving multi-platform projects, a module may be distributed across several compilations, projects and/or platforms.

For the purposes of Kotlin/Core, modules are important for internal visibility. How modules influence particular platforms is described in their respective sections of this specification.