Skip to content

Libraries

C272 edited this page Dec 18, 2019 · 3 revisions

In Algo, libraries are similar to objects in how they are created and accessed. However, libraries are created in their own completely separate scope (see Scopes in Algo), and can't interact with the parent scopes directly. They are commonly used for imported files to define a separate scope for the file. Below is how you define a library in Algo:

library myLib 
{
    ...
}

Inside the braces, you can write any statements you like, however they will be placed in this new "library scope", rather than in the current global scope. Here's a simple example of putting variables in libraries:

let y = 4;
library myLib 
{
    let x = 3;
    let z = y; //doesn't work, y is in a higher scope
}

print myLib.x; //works

You can also put libraries within other libraries, to create nested scopes, all of which are separate from their parent scopes.

library myLib
{
    let y = 4;
    library secondLib 
    {
        let x = 3;
        let z = y; //doesn't work, in a higher scope
    }

    print secondLib.x; //works
}

Libraries can access any value in the same scope or lower in the scope heirarchy. For example, here is a list of nested libraries:

(highest) global -> lib1 -> lib2 -> lib3 (lowest)

Global scope can access any values within all three libraries. However, lib2 can only access itself and lib3. Following the same principle, lib1 can only access itself, lib2 and lib3.