Skip to content

Language Concepts: Value holders (Variables and Constants)

Luiz Fernando edited this page Mar 26, 2015 · 4 revisions

Value holders (Variables and Constants)

Values in ZScript can be stored in constructs called 'value holders'. There are three types of value holders: Variables, constants and function parameters. Function parameters will be discussed in the 'Function' section, the semantics of variables and constants are as follow:

Variables

Variables are created by using the 'var' keyword, followed by the variable name, and, optionally, a type and a starting value:

var integer:int = 0; // Creates a variable named 'integer' that is of type int, containing a value 0

After creating a variable, you can use it in any place a value of its type is expected:

var num1:int = 5;
var num2:int = 6;

var sum:int = num1 + num2; // sum is now equals to 11, which is the sum of the values contained within num1 and num2

Variables are value holders that can have the value that they contain changed. This means that at any point, a variable may be assigned a different value

var integer:int = 0;
integer = 5;   // Set the contents of integer to 5
integer += 10; // Sum integer with 10, and assign the result to itself
// Now integer contains '15'

Optional-typed variables may also start value-less:

var integer:int?;
integer = 5;   // Set the contents of integer to 5
integer += 10; // Sum integer with 10, and assign the result to itself
// Now integer contains '15'

(Author note: the check for variable usage before initialization is not implemented yet, so trying to access the value of an uninitialised variable is not detected as a compile-time error and will result in runtime-errors)

Constants

Much like variables, constants are created by using the 'let' keyword, followed by the constant name, a starting value and, optionally, a type:

let count:int = 10; // Creates a constant named 'integer' that is of type int, containing a value 10

Constants are value holders that cannot have different values assigned to them after creation:

let count:int = 0;
count = 5;   // Error: Cannot re-assign constant after its creation
count += 10; // Error here too
count++;     // That's an error too, don't try to be sneaky and try to modify constants through increments!

Because constants cannot have their value changed, they, unlike variables, require an initial value to be provided at time of creation, otherwise an error is raised.

Note: Constants only stop you from modifying the reference of the object they point to, that means that if you e.g. create a constant list let l:[int], you can assign to the list through subscription l[0] = 1, though you cannot assign the constant value holder any other list

Type inferring

When creating variables and constants, the type of the value holder may be omitted. When that is done, the variable is still typed, but the type is now derived from the type of the initialization expression:

var integer = 10; // 'integer' was inferred the type of 'int'
let name = "Joe McDoe"; // 'name' was inferred type 'string'
integer = "sneakyString"; // Error: While 'integer' had no type provided, it was automatically inferred the type 'int', so no other value types can be assigned to it

Creating a type-less, valueless variable defaults the type of the value to 'any?':

var anything; // Equivalent to 'var anything:any?;'
anything = 10;
anything = "abc"; // Allowed, the variable 'anything' can hold any type due to the conditions it was initialized in