Co-authored-by: Ara Adkins <iamrecursion@users.noreply.github.com>
4.7 KiB
layout | title | category | tags | order | |||
---|---|---|---|---|---|---|---|
developer-doc | Imports and Exports | syntax |
|
4 |
Imports and Exports
In order to properly modularise and work with Enso code, the language provides a robust mechanism for importing code from modules, and also re-exporting that code from modules.
Import Syntax
There are two main ways of importing a module into the current scope.
Qualified Imports
These imports consist of the word import
followed by a qualified name of a
module. This can by optionally followed by the as
word, and a referent name of
the module as it should be visible in the importing scope.
The only name brought into scope by such an import is the name of the module (or
the name provided after the as
keyword, if provided).
Unqualified Imports
Unqualified imports are broken up into three main categories:
- Unrestricted Imports: These import all symbols from the module into the
current scope. They consist of the keyword
from
, followed by a qualified module name, followed by an optional rename part (using theas
keyword), then the keywordsimport all
. For example:from Base.List as Builtin_List import all
- Restricted Imports: These import a specified set of names for use as
automatically resolved referent names. They consist of the keyword
from
, followed by a qualified module name (with optionalas
-rename), then the wordimport
followed by a coma-separated list of referent names to be imported. For example:from Base.List import Cons, Nil
- Hiding Imports: These are the inverse of restricted imports, and import
all symbosl other than the named ones. They consist of the
from
keyword, followed by a qualified module name (with optionalas
-rename), then the wordsimport all hiding
, followed by a coma-separated list of referent names to be excluded from the import. For example:from Base.List import all hiding Cons, Nil
Imports in Enso may introduce ambiguous symbols, but this is not an error until one of the ambiguous symbols is used in Enso code.
Export Syntax
In order to allow for easy composition and aggregation of code, Enso provides its users with a mechanism to export imported elements from modules. They appear in Enso as follows:
Qualified Exports
These exports consist of the word export
followed by a qualified name of a
module. This can by optionally followed by the as
word, and a referent name of
the module as it should be visible in the exporting scope.
The only name brought into scope by such an export is the name of the module (or
the name provided after the as
keyword, if provided).
Unqualified Exports
Unqualified exports are broken up into three main categories:
- Unrestricted Exports: These export all symbols from the module into the
current scope. They consist of the keyword
from
, followed by a qualified module name, followed by an optional rename part (using theas
keyword), then the keywordsexport all
. For example:from Base.List as Builtin_List export all
- Restricted Exports: These export a specified set of names, behaving as
though they were redefined in the current scope. They consist of the keyword
from
, followed by a qualified module name (with optionalas
-rename), then the wordexport
followed by a coma-separated list of names to be exported. For example:from Base.List export Cons, Nil, from_vector
- Hiding Exports: These are the inverse of restricted exports, and export
all symbols other than the named ones. They consist of the
from
keyword, followed by a qualified module name (with optionalas
-rename), then the wordsexport all hiding
, followed by a coma-separated list of names to be excluded from the export. For example:from Base.List export all hiding from_vector, Nil
In essence, an export allows the user to "paste" the contents of the module being exported into the module declaring the export. This means that exports that create name clashes must be resolved at the export site.
Visibility of Export Bindings
Bindings exported from a module X
are available in an identical fashion to
bindings that are defined in the module X
.