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:
- Regular declaration with initialization:
- Type-hinted declaration with initialization:
var name: type1|type2 = value.
- Type-hinted declaration without initialization:
0 for integers, false for booleans, "" for strings, null for 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 unpacking multiple values into 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.
You can also unpack the elements of a list returned by a function:
Now, zero is 0 and one is 1.
Variable Scope¶
Kiwi uses lexical scoping. Variables created inside a function are local to that call. Variables created inside a control-flow block (if, while, for, repeat, do) are local to that block. See Scoping for the full rules.
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 ${}.