Importing

In Amber, you can import functions from other files. To make a function accessible from an external file, you need to declare it as public within the file where it’s defined.

Public Functions

To declare a function as public you can use a pub keyword. Keep in mind that pub keyword has to be used before the fun keyword that declares your function:

pub fun sum(left: Num, right: Num): Num {
    return left + right
}

Importing from other files

You can import each function individually...

import { foo, bar } from "./my-file.ab"

foo()
bar()

...or you can import all functions at once

import * from "./arith.ab"

echo sum(1, sub(2, mul(4, 5)))

Public imports

There are times when you might want to export what you imported. Then you can simply do this:

pub import * from "my/path/file.ab"

This will import all functions defined in file.ab and all of them will be publicly available again from this file.

Main block

Sometimes you want to run certain code when file is being run directly. The same issue can be solved in Python:

if __name__ == '__main__':
    # code to execute

Amber has a special syntax for this pattern. It is not just a syntactic sugar though. Main scope gives you also the ability to use ? operator in which case the exit code will be simply propagated to the external shell.

echo "Running indirectly"

main {
    $some command$?
    echo "Running directly"
}

Now if we run this file the output will look like this:

Running indirectly
Running directly

Here is the behavior when we import the file instead.

import * from "./file.ab"
// Outputs: Running indirectly

Main block and external arguments

Main block can provide an array of arguments (that is of type [Text]) passed to this script.

main (args) {
    loop i, arg in args {
        echo "{i}: {arg}"
    }
}