Variable Declaration & Assignment
Variables are used to store data in Harneet. Harneet provides flexible ways to declare and assign values to variables.
Explicit Type Declaration
You can explicitly declare the type of a variable when you declare it.
Type Inference
Harneet can infer the type of a variable based on the value assigned to it.
| Type Inference | |
|---|---|
Shorthand with Type Inference
Use var with an initializer to infer the type. This is the recommended shorthand for declaration + assignment.
Assignment
You can assign a new value to an existing variable using the = operator.
Zero Value Initialization
When a variable is declared without an explicit initial value, it is automatically initialized to its "zero value".
- Integer Types:
0 - Float Types:
0.0 - String Type:
""(empty string) - Boolean Type:
false
| Zero Values | |
|---|---|
Multiple Assignment
Harneet supports assigning multiple values to multiple variables in a single statement. This is particularly useful for functions that return multiple values.
Function Returns
| Function Return Values | |
|---|---|
Multiple Expressions
| Multiple Variable Declaration | |
|---|---|
Multiple Assignment (single line)
| Multiple Assignment (Single Line) | |
|---|---|
Blank Identifier
The blank identifier _ can be used to discard unwanted values in multiple assignment operations.