Skip to content
Bill Hails edited this page Oct 21, 2018 · 7 revisions

Files can be read in with the load directive. For example

load utils.sort;

utils.sort.qsort([2, 4, 2, 1, 5])

Internally that load statement is rewritten to

define utils = env extends globalenv {
    define sort = env extends globalenv {
        // contents of file ./utils/sort.fn
    }
}

Note that the environments are declared to extend globalenv, rather than whatever the current environment may be.

The file being loaded is only ever parsed once, and the AST is stored for later re-use, though it will be type-checked and evaluated separately for each load.

The load directive allows an alias to be provided for the package/environment being loaded, so:

load utils.sort as sorting;

sorting.qsort([2, 1, 3, 5, 4]);

That gets rewritten to:

define utils = env extends globalenv {
    define sort = env extends globalenv {
        // contents of ./utils/sort.fn
    }
};
define sorting = utils.sort;

sorting.qsort([2, 1, 3, 5, 4]);

Things get a bit more complicated when there are multiple imports. In fact all of the load statements are hoisted to the start of the current statement block and merged, so

load utils.sort;

fn foo(x) { x }

load utils.lists as lists;

is first rewritten to

load utils.sort;
load utils.lists as lists;

fn foo(x) { x }

then it becomes:

define utils = env extends globalenv {
    define sort = env extends globalenv {
        // contents of utils/sort.fn
    };
    define lists = env extends globalenv {
        // contents of utils/lists.fn
    }
};
define lists = utils.lists;

This is subject to change, we may only want the outermost env to extend globalenv.

Up: Home

Next: Then and Back

Clone this wiki locally