原因模块系统

tuc*_*hk4 5 ocaml reason

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.
Run Code Online (Sandbox Code Playgroud)

我试过推理模块系统,但无法理解它是如何工作的.

1)open和之间的区别是include什么?

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

我应该openinclude模块Foobar.re?或者直接访问 - Foo.someFunction

3)模块接口应该只实现ay *.rei文件?和模块接口文件应该是同名但有reiext?

gle*_*nsl 11

1)open就像是import,它将打开的模块中的导出定义添加到本地名称空间.include将它们添加到模块,就像您将包含模块中的定义复制到includee一样.ìnclude因此也将导出定义(除非有一个接口文件/签名限制导出的内容当然)

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

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

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