thp-lang.org/md/learn/basics/variables.md
2023-10-01 20:41:38 -05:00

57 lines
911 B
Markdown

# Variables
thp distinguishes between mutable and immutable variables.
## Mutable variables
Defined with `var`, followed by a variable name and a value.
```thp
var name = "John"
var age = 32
```
### Datatype annotation
Written after the `var` keyword but before the variable name.
```thp
var String name = "John"
var Int age = 32
```
When annotating a mutable variable the keyword `var` is _required_.
## Immutable variables
Defined with `val`, followed by a variable name and a value.
```thp
val surname = "Doe"
val year_of_birth = 1984
```
### Datatype annotation
Same as mutable variables
```thp
val String surname = "Doe"
val Int year_of_birth = 1984
```
When annotating an immutable variable the `val` keyword is optional
```thp
// Equivalent to the previous code
String surname = "Doe"
Int year_of_birth = 1984
```
This means that if a variable has only a datatype, it is immutable.