What is the difference between inclusion, requirement and opening in OCaml?

For instance,

include: include Ppx_core

open: open Core.Std

required: #require "compiler-libs.common"

and use: #use "topfind"

+6
source share
2 answers
  • include re-exports the components of the module in the current structure: the module you are in will contain all the definitions that are in Ppx_core .
  • open makes the components of the module available directly in the input environment. Instead of entering Core.Std.element you can simply enter element .
  • #require is a Topfind command that finds a library and loads it, making its modules available to you.
  • #use behave as if copying the full file directly to your belly button.

Note that the words # are not part of the OCaml language, but are top-level commands: they will not work if you try to compile your file.

+4
source

The include Module.Name in the module definition will include all the definitions from the module named Module.Name . Definitions will be included in the same way as they were copied. If include Module.Name occurs inside a module type definition (aka signature definition), Module.Name must be a valid (known to the compiler) module type. It will include determining the type of module as such (without including type constraints).

The open Module.Name , found both in the module implementation and in the module signature, allows you to refer to the definitions (values, types, submodules) of Module.Name without using a fully qualified name, that is, using short names without the Module.Name Prefix .

The #require is not an expression at all and is not part of the OCaml grammar. This is a special OCaml toplevel directive - an interactive loop. The same as ipython has its own directives. The require directive will load the specified package and all its dependencies. Moreover, this directive is not part of the standard OCaml distribution, but a topfind script is added, which is part of the ocamlfind toolkit. The #use directive #use used to load and evaluate a script. For example, #use "topfind" load and evaluate a topfind script from the OCaml standard library library. This script will register the require directive. There are also #load and #load_rec directives that work on a more subtle level, rather than in packages - these directives are designed to load libraries.

+2
source

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


All Articles