const
const, keyword, assignment option
The const keyword indicates that the value taken by a variable is known at compile time and that it won’t change later at runtime. Those variables are primarily intended to control the files being read.
const myFolder = "/foo"
read "\{myFolder}/products.csv" as Products with // pattern in 'read' must be constant
Product: text
Price: number
Notably, the read block requires a text value for its file path that evaluates as const.
Unlike many other languages, in Envision, const is a somewhat rarely used keyword, mostly used for certain capabilities that do not accept anything but compile-time constants (ex: read blocks).
Where const is required
Compile-time constants are required for:
readpatterns and bounded path schema constraints.loopiteration counts.matchpatterns (patterns cannot contain variables).- module exports flagged as
const. - expressions inside
def const purefunctions. - context capture values used by user-defined functions.
Constraints
Compile-time constants are scalars or tuples of scalars. Every tuple element
must be a compile-time constant. They cannot contain ranvar, zedfunc, or
embedding values.
Allowed constant expressions
Arithmetic and math operations:
const a = 10 + 25
Enum conversions:
table enum Currency = "EUR", "USD"
const EnumUSD = enum<<Currency>>("USD")
const TextUSD = text(EnumUSD)
Const pure functions:
const four = strlen("four")
Conditional expressions:
const label = if 1 < 2 then "yes" else "no"
Text interpolation:
const hello = "Hello"
const world = "World"
const greet = "\{hello}, \{world}!"
Tuples and unpacking:
const config = ("EUR", 2)
const currency, digits = config
show summary "Settings" a1d2 with
currency
digits
The tile shows “EUR” and 2. Tuples of constants can also be returned by
def const pure functions or exported from modules.
The constant() function is unrelated to const and does not create compile-time constants.
const, keyword, pure function option
The const keyword indicates that the pure function can be executed at compile time.
def const pure myInc(a: number) with
return a + 1
const a = 42
// if 'myInc' is called with a 'const' input, then, its output is 'const' too
const x = myInc(a)
const myFolder = "/foo"
read "\{myFolder}/products-\{x}.csv" as Products with // pattern in 'read' must be constant
Product: text
Price: number
In the present reference documentation, pure functions that are part of the Envision standard library and that can be executed at compile time are marked as const.