Variable initializers
Variables can be initialized with a literal.
The
DEFINE
instruction can be used with an equal sign followed by a literal to
initialize the variable:DEFINE v INTEGER = -800
Note: A variable initializer can not use expressions such as a value returned by a function, it must
always be a static literal.
The compiler produces the error -6631, if the type of the initializer and the type of the variable are
incompatible:
MAIN
DEFINE v INT = "abc"
| incompatible types, found: 'CHAR', required: 'INTEGER'.
| See error number -6631.
END MAIN
Note: Local function variables of the first function in a module will be initialized before any
WHENEVER
statement.Date variables can be initialized with a
MDY()
literal:DEFINE d DATE = MDY(12,24,2018)
Record variables can be initialized with a list of identifiers followed by a colon and the
literal value, surrounded with
parentheses:
DEFINE r1 RECORD
f1 INTEGER,
f2 STRING
END RECORD = (f1: 99, f2: "abc")
An invalid record initialization value can produce compilation errors such as -8421, -8424, or -8425.
Variables defined with types can also be
initialized:
TYPE Type1 RECORD
f1 INTEGER,
f2 STRING
END RECORD
DEFINE r2 Type1 = (f1: 99, f2: "abc")
Record members can be omitted, or specified in a different
order:
DEFINE r1 Type1 = (f2: "abc") -- f1 omitted
DEFINE r2 Type1 = (f2: "abc", f1: 99) -- f2 before f1
Note: When a record member is omitted in the initializer, it will get the default value according to
its type, as described in Variable default values.
Variables defined from database schema columns can
be initialized by specifying the initializer after the
LIKE
clause:SCHEMA stores
DEFINE customer RECORD LIKE customer.* = (fname: 'Peter', lname: 'Mango')
Initiliazers for nested records must specify the sub-record member name followed by parentheses
including the values for the members of the the
sub-record:
DEFINE r1 RECORD
f1 INTEGER,
f2 RECORD
f21 STRING,
f22 STRING
END RECORD
END RECORD = ( f1: 99,
f2: (f21:"abc", f22:"def") )
Arrays can be initialized by specifying a list of elements separated by a comma, surrounded by
square brackets:
DEFINE arr DYNAMIC ARRAY OF INT = [ 1, 2, 3, 5, 8, 13 ]
An invalid array initialization value can produce compilation errors such as -8422.
Structured arrays can be initialized by combining the initializer syntax for arrays and
records:
DEFINE pls DYNAMIC ARRAY OF RECORD
id INTEGER,
name VARCHAR(50)
END RECORD =
[
( id: 501, name: "Baxter" ),
( id: 502, name: "Folkap" ),
( id: 503, name: "Kirtshof" )
]
See the DEFINE syntax topic for a complete description of the variable initializer syntax.