Mutability and Assignment¶
Desi has a clear distinction between immutable and mutable variables, and between initial binding and mutation.
Variable Declaration¶
# Immutable variable (cannot be changed)
let name = "Alice"
let count = 10
# Mutable variable (can be changed)
let mut counter = 0
let mut data = [1, 2, 3]
Assignment Operators¶
| Operator | Usage | Description |
|---|---|---|
= |
Initial binding | Used with let to create new variables |
:= |
Mutation | Used to change existing values |
Initial Binding with =¶
let x = 10 # Bind x to 10
let message = "Hello" # Bind message to string
let nums = [1, 2, 3] # Bind nums to list
Mutation with :=¶
let mut x = 10
x := 20 # Mutate x from 10 to 20
let mut data = [1, 2, 3]
data[0] := 99 # Mutate list element
# Also works in class methods
self.value := new_value
Important
Always use := for mutation, never =. Using = for mutation will cause a compiler error.
Immutable vs Mutable¶
Immutable Variables¶
Mutable Variables¶
Class Fields¶
Fields can be mutable or immutable:
class Person:
pub name: str # Immutable field
pub mut age: int # Mutable field
pub def __new__(self, name: str, age: int):
self.name = name
self.age = age
pub def have_birthday(self):
self.age := self.age + 1 # OK - age is mutable
# self.name := "Bob" # ERROR - name is immutable
In Loops¶
Or with augmented assignment: