2017-06-05 60 views
4

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调用功能。

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

3)模块接口只能实现*.rei文件?和模块接口文件应该是相同的名称,但与rei分机?

回答

9

1)open就像import,它将打开模块中的导出定义添加到本地名称空间。 include将它们添加到模块,就好像您将包含的模块中的定义复制到包含模块一样。因此,ì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