Constant Expressions

Some constructs in Envision require their arguments to be known before the script begins reading input data. For instance, the path to be read by a read statement must naturally be known before the corresponding files can be located and read. In Envision, all computations that are performed before the actual processing begins are called compile-time constants, or const for short.

Values which must be compile-time constants include:

Compile-time constants are always scalars (vectors are never treated as constants, even if they have the same constant value on every line) and may not be of type ranvar, zedfunc, or embedding.

Roadmap: compile-time constants cannot currently be tuples, but support for constant tuples is planned.

The simplest compile-time constant expressions are literals such as 1, false or "Hello". For more complex expressions, Envision will check that they only perform operations allowed in a compile-time constant context:

 ⇒ Calling arithmetical operators and mathematical functions.

const a = 10 + 25

 ⇒ Converting text to and from a static enum.

table enum Currency = "EUR", "USD"
const EnumUSD = enum<<Currency>>("USD")
const TextUSD = text(EnumUSD)

 ⇒ Calling pure functions explicitly flagged as const, including user-defined functions manually flagged as such, as well as standard library functions. For the latter, the const flag is included in the documentation page for those functions (see for example strlen()).

const four = strlen("four")

 ⇒ Conditional if .. then .. else expressions.

const isMonday = if today() == monday(today()) then "Monday" else "Not Monday"

 ⇒ Accessing a variable flagged as const, whether that variable is imported from a module, defined in the same script with a const assignment, or read from a form:

// Module named /myModule
export const a = 10
import "/myModule" as M

const b = 20

read form with 
  c : number
  
const d = M.a + b + c

 ⇒ Text interpolation.

const hello = "Hello"
const world = "World"
const greet = "\{hello}, \{world}!"

Note that the the constant() function is not related to the const keyword, and does not refer to any kind of compile-time constants.

User Contributed Notes
0 notes + add a note