declare
Syntax
Every variable has a type, which defines the kind of values it can store. You can explicitly
specify the type by providing a <variable type> after the as
keyword.
The initial value of the variable can be set by using an initialization expression
(<variable value>) following the being keyword. If no type is
specified, the compiler automatically infers the variable type from this initialization
expression.
Optionally, you can also define a <default value> using the
defaulting to keyword. This default value is used whenever no value is provided or
when the provided value is null.
Declare with an initial value
Declares a variable and initializes it with a value. The variable type is inferred from the value.
- Syntax
declare 'variable name' being <variable value> ;- Example
declare x being 10 ;- Expected output
xis a number with the value10.
Declare with an explicit type
Declares a variable with a specified type. The variable is initialized to null
by default.
- Syntax
declare 'variable name' as <variable type> ;- Example
declare text as a string ;- Expected output
textis a string initialized tonull.
Declare with a type and an initial value
Declares a variable with both an explicit type and an initial value.
- Syntax
declare 'variable name' as <variable type> being <variable value> ;- Example
declare text as a string being "John Doe" ;- Expected output
textis a string initialized to"John Doe".
Declare with a default value
Declares a variable with a type, an initial value, and a fallback default value. The default
value is used if the initial value is null or not provided.
- Syntax
declare 'variable name' as <variable type> being <variable value> defaulting to <default value> ;- Example
declare text as a string being result defaulting to "John Doe" ;- Expected output
textis a string initialized to the value ofresultifresultis notnull. Otherwise, it is set to"John Doe".