Skip to content
Neuron Teckid edited this page Mar 9, 2017 · 1 revision

Include

Flatscript allows including other Flatscript source files as modules.

Syntax

include 'path/to/file' as module_name

where 'path/to/file' ought to be a file path that is accessible to the compiler, relative to the compiler program path if it is a relative path.

Though it uses the include keyword, it does not mean the included source is expanded and replace the include statement. Actually, the compiler will separated compile included source, and consider it as a object, whose properties are exported names in that source file.

For example, the content in "a.fls" is

export func foo(x)
    return x + 1

the content in "b.fls" is

include 'a.fls' as a
console.log(a.foo(2)) # compile b.fls and output at this line is: 3

If a file gets included multiple times, either in one file or in different files, it is processed only once. For example, adding a "c.fls" with the following content

include 'a.fls' as a
include 'b.fls' as b
console.log(a.foo(3)) # compile c.fls and output at this line is: 4

When compiling 'c.fls', the compiler caches the compile result of 'a.fls', so that 'a.fls' is not going to be processed again when compiling 'b.fls'.

Two source files (or more) should not include each other, and a file should not include itself. Otherwise, a compile error occurs.