#import
The #import
directive is specific to the LIGO compiler. It provides
the support for a minimal module/namespace system.
For more information about namespaces, see Namespaces.
Namespaces get more handy when they can be made from a file, separate from our own program, like a library: when we import a namespace from such a file, we automatically obtain a namespace encapsulating all the definitions in it. This will become very handy for organising large contracts, as we can divide it into different files, and the namespace system keeps the naming space clean (no need for name mangling).
Generally, we will take a set of definitions that can be naturally
grouped by functionality, and put them together in a separate
file. For example, we can create a file euro.jsligo
:
In another file, we can import euro.jsligo
as a namespace, and use
its definitions. For example, we can create a main.jsligo
that
imports all definitions from euro.jsligo
as the namespace Euro
:
Importing namespaces
When you import a file with the #import
directive, LIGO packages the file as a namespace.
Therefore, any namespaces in the file are sub-namespaces of that namespace.
However, the namespace does not export those sub-namespaces automatically. As a result, if you import a file that contains namespaces, those namespaces are not accessible.
To work around this limitation, add the @public
decorator to the namespaces in the file.
For example, this file defines the Euro type as a namespace with the @public
decorator:
Because the namespace is public, you can access it as a sub-namespace when you import the file into another file: