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 always scalars. They cannot be ranvar, zedfunc, or embedding, and tuples are not supported.
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}!"
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.