DOCUMENTATION

Variables & Constants

DOCUMENTATION

XCX 4.4.1 Variables and Constants

Variable Declarations

Variables are declared using the type: name = value; syntax.

$ xcx
_ ⛶ ×
1i: age = 25;
2f: pi = 3.14159;
3s: name = "XCX";
4b: is_ready = true;

Variables can also be declared without an initial value, in which case they take the default value for their type (e.g., 0 for i, "" for s).

Multiple Variable Declarations

Basic types support declaring multiple variables of the same type in a single statement, separated by commas. Each variable can optionally have its own initializer:

$ xcx
_ ⛶ ×
1i: x, y, z; --- Declares x, y, z as integers (default 0)
2f: a = 1.0, b, c; --- Declares a (1.0), b (0.0), and c (0.0)
3s: first, middle, last = "Doe";

NOTE:

A trailing initializer binds only to the last declared name. In the

example above, first and middle take the type default ("") and only

last receives "Doe". To set several variables in one statement, give

each its own initializer: s: a = "x", b = "y";.

Reassignment

Once declared, variables can be reassigned using the = operator.

$ xcx
_ ⛶ ×
1i: count = 0;
2count = count + 1;

Constants

Constants are declared using the const keyword. They must be initialized at declaration and cannot be changed afterwards.

$ xcx
_ ⛶ ×
1const i: MAX_CONNECTIONS = 1024;
2const s: VERSION = "2.0.0";

No Variable Shadowing

XCX does not support variable shadowing. Declaring a variable with the same name in any scope - including a nested block - is a compile-time error (RedefinedVariable).

$ xcx
_ ⛶ ×
1i: x = 10;
2if (true) then;
3 i: x = 20; --- COMPILE ERROR: RedefinedVariable
4end;

If you need to change a value inside a block, use reassignment instead of redeclaration:

$ xcx
_ ⛶ ×
1i: x = 10;
2if (true) then;
3 x = 20; --- OK: reassignment to existing variable
4 >! x; --- 20
5end;
6>! x; --- 20 (the global variable was modified)