Reason module system
Asked Answered
B

1

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.

I tried reason modules system but can't understand how it works.

1) What the differents between open and include?

2) I have file foo.re with defined module Foo. I have file bar.re and want to call function from module Foo.

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

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

Boiardo answered 5/6, 2017 at 20:43 Comment(0)
Q
12

1) open is like import, it adds the exported definitions in the opened module to the local namespace. include adds them to the module as if you copied the definitions from the included module to the includee. ìnclude will therefore also export the definitions (unless there's an interface file/signature that restricts what's exported of course)

2) you should prefer the most local use of a module that is convenient, in order to not unnecessarily pollute the namespace. So typically you'll want to use direct access, and only if a module has been specifically designed to be opened at file level should you do so. There are however forms of open that are more local than file level. You can open a module in just the scope of a function, or even scoped to a single expression in the form of Foo.(someFunction 4 |> otherFunction 2)

3) Toplevel (file) modules must be implemented in the form of an rei file with the same name as the re file. You can however define module types as "interfaces" for submodules.

OCaml's module system is quite extensive and flexible. I recommend reading the module chapter of Real World Ocaml to get a better grasp of it: https://realworldocaml.org/v1/en/html/files-modules-and-programs.html

Quell answered 5/6, 2017 at 21:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.