Variable initializers
Variables can be initialized in their definition.
Syntax
variable-definition = initializer
{
scalar-initializer
|
record-initializer
|
array-initializer
|
dictionary-initializer
}
{
integer-literal
|
numeric-literal
|
text-literal
|
mdy-date-literal
|
datetime-literal
|
interval-literal
|
boolean-literal
|
NULL
}
( identifier : initializer [
,...]
)
[ initializer [
,...]
]
( key : initializer [
,...]
)
- variable-definition is a
DEFINE
orVAR
instruction. - integer-literal is an whole number literal.
- numeric-literal is a decimal number literal.
- text-literal is a character string literal, including localized strings.
- mdy-date-literal is an
MDY(mm,dd,yyyy)
specification. - datetime-literal is a datetime literal.
- interval-literal is an interval literal.
- key identifies a dictionary entry. It must be a string literal, and cannot be a localized string.
Variable initializer basics
DEFINE
instruction, add an equal sign followed
by an initializer literal:DEFINE v INTEGER = -800
A variable initializer can not use expressions such as a value returned by a function, it must always be a static literal.
MAIN
DEFINE v INT = "abc"
| incompatible types, found: 'CHAR', required: 'INTEGER'.
| See error number -6631.
END MAIN
Local function variables of the first function in a module will be initialized before any
WHENEVER
statement.
See the DEFINE syntax topic for a complete description of the variable initializer syntax.
Initializing DATE variables
MDY()
literal:DEFINE d DATE = MDY(12,24,2018)
Initializing RECORD variables
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.
TYPE Type1 RECORD
f1 INTEGER,
f2 STRING
END RECORD
DEFINE r2 Type1 = (f1: 99, f2: "abc")
DEFINE r1 Type1 = (f2: "abc") -- f1 omitted
DEFINE r2 Type1 = (f2: "abc", f1: 99) -- f2 before f1
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.
LIKE
clause:SCHEMA stores
DEFINE customer RECORD LIKE customer.* = (fname: 'Peter', lname: 'Mango')
DEFINE r1 RECORD
f1 INTEGER,
f2 RECORD
f21 STRING,
f22 STRING
END RECORD
END RECORD = ( f1: 99,
f2: (f21:"abc", f22:"def") )
Initializing DYNAMIC ARRAY variables
DEFINE arr DYNAMIC ARRAY OF INT = [ 1, 2, 3, 5, 8, 13 ]
An invalid array initialization value can produce compilation errors such as -8422.
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" )
]
DEFINE matrix1 ARRAY[5,3] OF INTEGER = [
[11,12,63],
[58,23,63],
[21,67,36],
[11,65,31],
[14,12,32]
]
Initializing DICTIONARY variables
DICTIONARY
variables can be initialized by using a comma-separated list of
elements included in parentheses. Each element must be a key/value pair separated by a
colon:DEFINE mcs DICTIONARY OF DECIMAL(10,5) =
( "Pi" : 3.14159,
"Euler" : 2.71828,
"Golden" : 1.61803
)
Structured dictionaries can be initialized by combining the initializer syntax for dictionaries and records:
DEFINE pls DICTIONARY OF RECORD
name VARCHAR(50),
address VARCHAR(200)
END RECORD =
(
"CB841" : ( name: "Baxter", address : "4 Baker Street" ),
"CB112" : ( name: "Folkap", address : "234 Sunset Road" ),
"CB233" : ( name: "Kirtshof", address : "76 Colmor Row" )
)