Variables
A variable is a named representation of some section of memory. Variables are used to store values, one of the most fundamental part of any program. Some examples of declaring variables are:
1
var my_variable = 123
2
var my_variable: Number = 123
3
4
let my_variable = 123
5
let my_variable: Number = 123
6
7
const my_constant = 123
8
const my_constant: Number = 123
Copied!
let is simply an alias for var, they are exactly the same.

Strong v Weak

Many, now considered, "low-level" programming languages such as C are "strongly" typed. This means, you cannot change the type of a variable once it has been declared. This type is passed with the variable's decleration. For example the following is invalid:
1
int foo = 123; // Declares `foo` as an integer;
2
foo = "bar"; // Error: cannot make an int a string
Copied!
Note: This works as well (since version v1.0.0-beta.51)
1
var foo := 123; // Declares `foo` as an integer
2
foo = "bar"; // Error: cannot make an int a string
Copied!
Some other languages such as JavaScript and Python, are "weakly" typed, this means you can change the type of a variable after it has been declared:
1
var foo = 123;
2
foo = "bar"; // just fine
Copied!
Cheddar uses both. Different programmers have different preferences on which typing is the best. Strong typing can be used as beneficial to avoid hard to debug bugs when typed automatically change. Weak typing can be helpful when quickly scripting. Cheddar suits both:
1
// == Weak Typing ==
2
var weak_var = 123
3
weak_var = "bar" // no error
4
5
// == Strong Typing ==
6
var strong_var : Number = 123
7
strong_bar = "bar" // error
Copied!
Note: If you're using a Cheddar version < 0.3 (cheddar -V) implicit variables use the legacy var a := b syntax.

Constants

Often times you want a variable that doesn't change, to be a "constant". Cheddar supports this, simply instead of the var keyword, use const:
1
const my_constant = 123
2
my_constant = "foo"; // Error
Copied!
Constants can also be strongly & weakly typed, except that the only time the type checking will ever happen is at the deceleration.
Last modified 1yr ago
Copy link