Variables¶
Variables are by default dynamically typed, meaning they do not require explicit type declarations. The type of a variable is inferred from the value assigned to it. For more information on types, please see Types.
var Keyword¶
The var keyword in Kiwi simplifies variable declaration, supporting multiple styles of initialization with optional type hints.
Syntax¶
Single variable:
Multiple variables (grouped with parentheses):
Declaration Options¶
Each declaration can take one of the following forms: 1. Regular declaration with initialization:
- Infers the variable type from the assigned value.- Type-hinted declaration with initialization:
- Specifies the variable type explicitly with an initializer.
-
Union types are supported:
var name: type1|type2 = value. -
Type-hinted declaration without initialization:
-
Defaults to the type’s default value (e.g.,
0for integers,falsefor booleans,""for strings,nullfor others). -
Uninitialized declaration:
- Defaults to
null.
Multiple Declarations¶
You can group multiple declarations within parentheses () separated by commas ,:
Examples¶
Single Declaration¶
var x = 42 # `x` is an integer with the value 42
var name: string # `name` is a string, defaults to ""
var flag: boolean = true # `flag` is a boolean with the value true
Multiple Declarations¶
var (
age: integer = 25, # `age` is an integer with value 25
name = "Kiwi", # `name` is a string with value "Kiwi"
is_active: boolean, # `is_active` is a boolean, defaults to false
count # `count` is uninitialized, defaults to null
)
Practical Example¶
fn initialize_variables()
var counter: integer = 0
var name: string = "Test"
var active: boolean = true
var data: list = [1, 2, 3]
var metadata: hashmap = {"key": "value"}
return [counter, name, active, data, metadata]
end
Key Features¶
- Type Hints: Declare variables with explicit types for clarity and error prevention.
- Initialization Flexibility: Mix initialized and uninitialized variables.
- Compact Syntax: Group related variable declarations with
var (...)for cleaner, more maintainable code.
Use Cases¶
- Convenient Debugging: Quickly declare multiple variables for experimentation or testing.
- Readable Code: Clearly communicate intent through grouped declarations and type hints.
- Efficient Initialization: Avoid repetitive
nullinitializations or redundant type declarations.
Assigning Variables¶
You can assign a variable in Kiwi using the = operator.
For example:
a = 10 # Integer assignment
b = 20.5 # Float assignment
c = "Hello" # String assignment
d = [1, 2, 3] # List assignment
In these cases:
- a holds an integer value.
- b holds a floating-point number.
- c holds a string.
- d holds a list of integers.
Variable Reassignment¶
You can reassign variables at any point, and the type can change based on the new value:
Multiple Assignments¶
Kiwi supports multiple variable assignments using the unpacking syntax =<. This allows assigning multiple values to multiple variables in a single line:
Here:
- a is assigned the boolean value true.
- b is assigned a hashmap with a single key-value pair.
- c is assigned a list containing three integers.
This can also be used with functions that return multiple values:
Now, zero is 0 and one is 1.
Variable Scope¶
Variables in Kiwi are function-scoped, meaning they are only accessible within the function where they are defined unless passed as arguments or returned from a function.
For example:
String Interpolation with Variables¶
Kiwi supports string interpolation, allowing you to embed variable values directly into strings using ${} syntax:
This interpolation works with any valid expression inside ${}.