modules
Modules are reusable Envision code blocks imported by scripts or other modules. Modules are not executable: they do not
produce dashboards and cannot contain read, write, or show statements.
Only exported elements are visible outside the module. Non-exported values remain local to the module.
Export and import
The export keyword marks elements that can be imported elsewhere. The import keyword loads those elements.
// Module "/sample/my-module"
export const myHello = "Hello"
export const myWorld = "World!"
import "/sample/my-module" as M with
myHello
myWorld
show scalar "" with "\{myHello} \{myWorld}"
When a module is imported without a with block, its elements are accessed through the module alias as a namespace.
import "/sample/my-module" as M
show scalar "" with "\{M.myHello} \{M.myWorld}"
Exported vector functions
Modules can export pure, process, and vector functions. An exported
vector function is declared with export def vector. Its table parameters
and vector arguments are supplied by the importing script, so the module
does not need to read the caller’s data itself.
Call the function through the module alias, or import its name with a with
block. Both forms retain the function<<Tables>>(arguments) syntax. See
exporting a vector function
for the paired module and caller example.
Inline documentation
Triple-slash comments (///) immediately preceding an exported element are attached as inline documentation.
// Module "/sample/my-docs"
/// Greeting prefix.
export const myHello = "Hello"
Chained imports
A module can import another module. Circular dependencies are rejected.
// Module "/sample/my-base"
export const myNumber = 10
// Module "/sample/my-next"
import "/sample/my-base" as M
export const myNumber = 13 + M.myNumber
Type annotations on import
Import entries may re-specify types for clarity.
// Module "/sample/my-types"
export const myText = "Hello World!"
export const myNumber = 42
import "/sample/my-types" as M with
myText : text
myNumber : number
show summary "" with myText, myNumber
Export limitations
Only scalar values, tuples of scalar values, user-defined functions, enums, table comprehensions, and schemas can be exported.
Exported module values and exported table comprehensions must resolve to
compile-time constants. Consequently, runtime-built values such as ranvar,
zedfunc, and embedding cannot be exported as module values or inside
exported tables.
Exported functions and exported schemas may still return
or declare those types.