Overview - Datatypes
As previously shown in the docs page Variables, every variable always has a type that defines what values it can store. This also means that you often can not mix variables of different data types together, as they fundamentally represent different things.
What is a datatype?
A data type defines the type of value, which can be stored in a variable or constant.
A variable with the datatype num
, for example, can only contain numbers. A variable
with an str
datatype can only contain text, symbols or numbers, but saves them as text.
This makes them for example impossible to use for calculations and as such using them in arithmetic
expression is invalid.
Important
Data types can not be mixed together and must be converted before being used with another type.
✓ VALID CODE
// ✓ Valid
var var1: str = "This ";
var var2: str = "is ";
var var3: str = "a string";
var result: str = var1 + var2 + var3; // -> "This is a string"
// ✓ Also Valid
var var4: str = "42";
var var5: num = (var4 as num) + 5; // Converts the string to 'num' and adds 5 to them
// ✓ Also Valid
var var6: num = 32;
var var7: num = (var6 as num) \* 2; // Converts the string to 'num' and multiplies it by 2
X INVALID CODE
// X Invalid - May not re-define with new type signature
var var1: str = "3";
var var1: num = 3;
// X Invalid - Invalid conversion from 'str' to 'num'
var var2: str = "Obviously not a number";
var var3: num = var4 as num; // Impossible to convert!
// X Invalid - Invalid conversion from 'str' to 'num'
var var2: str = ""; // empty
var var3: num = var2 as num; // Impossible to convert, as it is an empty value!