Skip to content

Variables

LordPos edited this page Aug 29, 2020 · 2 revisions

A variable is a name given to a storage area that the programs can manipulate. Each variable in Volant has a specific type, which determines the size and layout of the variable's memory, the range of values that can be stored within that memory, and the set of operations that can be applied to the variable.

The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because Volant is case-sensitive.

Declaring a variable

Variables are declared with the syntax variableName: variableType;. For example, var: i32; declares a variable of name var with a type of 32bit signed integer. Multiple variables can be declared in the same statement by separating them with commas. For example,

var1, var2, var3: u8, u16, u32; // declares var1 of type u8, var2 of type u16 and var3 of type u32
var4, var5, var6: u64; // declares var4, var5 and var6 of type u64

var7, var8, var9: i8, i16; // error: too few types specified in declaration 

Initializing a variable

Initialization is done using the = operator. For example,

var: i32; // declare var 
var = 0; // initialize var with value 0

Multiple variables can be initialized in the same statement by separating them with commas:

var1, var2, var3: u8, u16, u32;
var1, var2, var3 = 0, 1, 2;

Variables can also be declared and initialized in a single statement using the syntax variable: type = value;. For example, var: i32 = 0; declares a variable of type i32 and initializes it with value 0.

Variables can also be implicitly declared and initialized by not supplying the type in the statement using the syntax variable := value. In such cases, the variable automatically takes the type of value on the right-hand side. For example, var := 0; creates a variable of type i32 with value 0.

Multiple variables can be declared and initialized using by separating them with commas. For example,

var1, var2, var3: type1, type2, type3 = val1, val2, val3; // explicit declaration
var4, var5, var6 := val5, val6, val7; // implicit declaration

Variables can be re-initialized as many times as needed.

Clone this wiki locally