原因模块系统

Reason module system

https://facebook.github.io/reason/modules.html#modules-basic-modules

I don’t see any import or require in my file; how does module resolution work?

Reason/OCaml doesn’t require you to write any import; modules being referred to in the file are automatically searched in the project. Specifically, a module Hello asks the compiler to look for the file hello.re or hello.ml (and their corresponding interface file, hello.rei or hello.mli, if available).
A module name is the file name, capitalized. It has to be unique per project; this abstracts away the file system and allows you to move files around without changing code.

我尝试了原因模块系统,但无法理解它是如何工作的。

1) openinclude有什么区别?

2) 我有文件 foo.re 和已定义的模块 Foo。我有文件 bar.re 并想从模块 Foo 调用函数。

我应该在 bar.re openinclude 模块 Foo 吗?或者只是直接访问 - Foo.someFunction?

3) 模块接口应该只在 *.rei 文件中实现?模块接口文件应具有相同的名称,但带有 rei ext?

1) open 类似于import,它将打开的模块中导出的定义添加到本地命名空间。 include 将它们添加到模块中,就像您将定义从包含的模块复制到被包含的模块一样。 ìnclude 因此也会导出定义(当然,除非有一个接口 file/signature 限制导出的内容)

2) 您应该更喜欢在最本地使用方便的模块,以免不必要地污染命名空间。因此,通常您会希望使用直接访问,并且只有当模块专门设计为在文件级别打开时才应该这样做。但是,open 的形式比文件级别更本地化。您可以 open 仅在函数范围内的模块,甚至可以将范围限定为 Foo.(someFunction 4 |> otherFunction 2)

形式的单个表达式

3) 顶级(文件)模块必须以与re 文件同名的rei 文件的形式实现。但是,您可以将模块类型定义为子模块的 "interfaces"。

OCaml 的模块系统相当广泛和灵活。我建议阅读 Real World Ocaml 的模块章节以更好地掌握它:https://realworldocaml.org/v1/en/html/files-modules-and-programs.html