Modular mind system

https://facebook.imtqy.com/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. 

I tried the markup module system, but I can’t understand how this works.

1) What are the differences between open and include ?

2) I have a file foo.re with a specific module Foo . I have a bar.re file and you want to call a function from the Foo module.

Should I open or include Foo module in bar.re ? Or just direct access - Foo.someFunction ?

3) Module interfaces should be implemented only in ay *.rei ? And the module interface file should be with the same name, but with rei ext?

+5
source share
1 answer

1) open is similar to import , it adds the exported definitions to the open module in the local namespace. include adds them to the module, as if you copied the definitions from the included module into the includeee. ìnclude will also export definitions (if there is no file / interface signature that limits what is exported, of course)

2), you should prefer the most local use of the module, which is convenient so as not to pollute the namespace unnecessarily. Thus, you usually want to use direct access, and only if the module was specifically designed to open at the file level, if you do. However, there are open forms that are more local than the file level. You can open module only in the scope of a function or even tied to a single expression in the form Foo.(someFunction 4 |> otherFunction 2)

3) Toplevel modules (file) must be implemented as a rei file with the same name as the re file. However, you can define module types as “interfaces” for submodules.

The modular system of OCaml is quite extensive and flexible. I recommend reading the chapter in the Real World Ocaml module to better understand it: https://realworldocaml.org/v1/en/html/files-modules-and-programs.html

+10
source

Source: https://habr.com/ru/post/1268565/


All Articles