Imports And Exports
An Aqua source file has a head and a body. The body contains function definitions, services, types, constants. The header manages what is imported from other files and what is exported.

Module

By default, .aqua file exports and declares everything it contains. With module header you can describe the .aqua file's interface.
1
-- Module expression may be only on the very first line of the file
2
module ModuleName declares *
Copied!
Module.Name may contain dots.
ModuleName can be used as the module's name when this file is used. In this case, only what is enumerated in declares section will be available. declares * allows you to declare everything in the file as the module interface.
1
module ModuleName declares CONSTNAME, ServiceName, MyType, fn
2
​
3
const CONSTNAME = "smth"
4
​
5
service ServiceName:
6
do_smth()
7
8
data MyType:
9
result: i32
10
​
11
function fn() -> string:
12
<- CONSTNAME
Copied!

Import Expression

The main way to import a file is via import expression:
1
import "@fluencelabs/aqua-lib/builtin.aqua"
2
​
3
func foo():
4
Op.noop()
Copied!
Aqua compiler takes a source directory and a list of import directories (usually with node_modules as a default). You can use relative paths to .aqua files, relatively to the current file's path, and to import folders.
.aqua extension in import and use expressions can be ommited. So, import "builtin.aqua" does exactly the same as import "builtin".
Everything defined in the file is imported into the current namespace.
You can cherry-pick and rename imports using import ... from expression:
1
import Op as Noop from "@fluencelabs/aqua-lib/builtin"
2
​
3
func foo():
4
Noop.noop()
Copied!

Use Expression

The use expression makes it possible to import a file into a named scope.
1
use Op from "@fluencelabs/aqua-lib/builtin" as BuiltIn
2
​
3
func foo():
4
BuiltIn.Op.noop()
Copied!
If the imported file has a module header, from and as sections of use may be omitted.
1
use "@fluencelabs/aqua-lib/builtin.aqua"
2
-- Assume that builtin.aqua's header is `module BuiltIn declares *`
3
​
4
func foo():
5
BuiltIn.Op.noop()
Copied!

Export

While it's useful to split the code into several functions into different files, it's not always a good idea to compile everything into the host language.
Another problem is libraries distribution. If a developer wants to deliver an .aqua library, he or she often needs to provide it in compiled form as well.
export lets a developer decide what exactly is going to be exported, including imported functions.
1
import bar from "lib"
2
​
3
-- Exported functions and services will be compiled for the host language
4
-- You can use several `export` expressions
5
export foo as my_foo
6
export bar, MySrv
7
​
8
service MySrv:
9
call_smth()
10
11
func foo() -> bool:
12
<- true
Copied!
Last modified 1mo ago