Sfairadora

  • Reference Guide

Definition of Named Constant

In Enki, a named constant can be defined using the def construct:
def flags name = value
The name must be an identifier. It is recommended to name constants in upper-case to distinguish them from variables.
The value must be specified by a constant expression (i.e. it cannot contain reference to variables and external data). Examples of constant expressions are: 5, 2+3, sin(20), etc. The type of the named constant is determined by the type of its value. If necessary, the type of the value can be enforced by the typecast operator.
The flags can be empty. The usage of the flags is described in the section L-Value Constants below.
As all Enki language constructs return a value, the value returned by a definition of constant is its value.

Compound Value Constants

The value of a constant can also be a compound type value
Example:
def SQUARES = [Seq(Int)]{0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100};
With this definition, the following expression can be written:
SQUARES[5]
The value of this expression is expression is 25. It is a constant expression.

Definition of Named Types

As a data type is a specific kind of value in Enki, it is possible to define a named type as a constant.
Example:
def Person = Struct(String name, String addr, String city);
var Person sender;
odesílatel.name = "Jan Novak";
odesílatel.addr = "Steinerova 12";
odesílatel.city = "Prague";
It is recommended to use names with the first character upper-case for named types.

L-Value Constants

With the def statement, also an l-value can be named (see the chapter Data References). In a sense, an l-value constant present an abbreviation for a longer reference to a data item. An l-value constant is defined by writing & or const & within the flags. In the latter case a constant l-value is defined – that is, the value of the denoted data item can be read using the constant but not modified. (But it does not prevent the data item from being changed other way).
Example:
def &item = document.data.customers.0.name;
With this definition, the expression item = "Adam" is equivalent to document.data.customers.0.name = "Adam".

Scope of Constant

A constant is accessible from the place it is defined onwards up to the end of function or end of block. The blocks are described in the chapter on variables. If it is desired to define a constant accessible in several functions (provided the functions are located at dispels), it can be done in a superordinate data provider. A document-wide constant can be created in the document section Definitions.