5

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.

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

open1)和之间有什么区别include

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

我应该open还是include模块Foobar.re或者只是直接访问 - Foo.someFunction

3)模块接口应该只实现y*.rei文件吗?并且模块接口文件应该具有相同的名称但带有reiext?

4

1 回答 1

11

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

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

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

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

于 2017-06-05T21:06:08.017 回答