2017-06-05 59 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