Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reason module system

Tags:

ocaml

reason

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?

like image 329
tuchk4 Avatar asked Jun 05 '17 20:06

tuchk4


People also ask

What is module system?

A module is a distinct assembly of components that can be easily added, removed or replaced in a larger system. Generally, a module is not functional on its own. In computer hardware, a module is a component that is designed for easy replacement.

What are the advantages of the SOM over a chip down design?

For low to mid volume projects, SOMs offer a more cost-effective solution than chip-down board production, with a faster time to market and long product lifecycle. A SOM is combined with a custom baseboard to give you complete control over the full system configuration.

What does a module do in Java?

A Java module is a set of packages that declares which of them form an API accessible to other modules and which are internal and encapsulated — similar to how a class defines the visibility of its members. A module also declares what other modules it requires for its operation.

What is a module in college?

A Module is a group of courses (usually 2000-level and above) that provides a certain level of understanding in a subject area. Western offers 4 types of modules in the regular undergraduate degrees (e.g. BA, BSc, BMSc degrees, etc.).


1 Answers

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

like image 172
glennsl Avatar answered Sep 27 '22 18:09

glennsl