Variables
Variable Declaration, Assignment and Explicit Typing.
Syntax
Declaration
Variable Declaration is inspired by JavaScript using the var / const keyword
var a = 10var / const Keyword
varis used to declare a variable that can be reassigned.constis used to declare a variable that cannot be reassigned.
Here the type of variable is inferred implicitly.
However we also provide support for explicit typing using the Colon syntax.
var a : int = 10This syntax is quite similar to TypeScript
Assignment
The variables can be reassigned.
a = 20Errors
In implicit typing, after type is inferred during declaration, reassigning it to another type later is not allowed and will throw an error:
>> var a = 10
10
>> a = "3dubs"
(1,1,1,1): Cannot convert type string to int implicitly.
a = "3dubs"Explicit typing also follows same convention as Implicit typing, but enforces the initial type during declaration.
>> var a : int = "3dubs"
(1,1,1,1): Cannot convert type string to int implicitly.
var a : int = "3dubs"Attempting to reassign a const variable will throw an error:
>> const x = 10
10
>> x = 11
(1,1,1,2): Variable 'x' is read-only and cannot be assigned to.
x = 11