Module
A Module
is a collection of types grouped within a namespace, which can be view as a functor, mapping type-level and term-level abstractions into structured instances. Therefore, a module is naturally a type with algebraic properties.
Namespace
Declare a module
By staring with namespace <ModuleName>
in a file, we declared the whole file as a module
In more general case, we can define a module inside a file, using curly brackets to explicitly delimit its content
Export
Definitions inside a module are private by default. When you import
a module, it will not expose any of its internal definitions unless they are explicitly marked with the export
keyword.
Export a type
Export multiple types
Example
Import
The
import
keyword brings types from other module into the current context.
Import whole module
A
<modlue>
is a logical namespace identifier, not a real file path. It is resolved into a file by combining:
- A search base path and
- The namespace name declared by the
namespace
keywordResolution Rules:
To locate a module, the resolver searches in the following order:
- Relative to the current file, or
- Under paths defined in the
ARCH_PATH
environment variableor from another package:
- Identify by a
@
prefix
Example
Import a module and assign it an alias
this is equivialent as:
Example
Using
The
using
keyword exposes types from a module to the current context, so you can reference them without a module prefix.
Expose the whole module
This form should be reserved for specific cases only such as using @common
, as it injects all exported names from the module into the current context, potentially leading to namespace pollution and ambiguity.
Expose seleceted types from a module
Example