Mutability
Mutability is a core concept in Ki; its safety features depend upon controlling the mutability of memory. For all of its responsibility however, mutability is quite simple.
Everything Is Immutable
Everything in Ki starts life immutable:
fn main() {
var charlie: Person("Charlie", 33) # Immutable
var hillary: Person("Hillary", 68) # Immutable
var barack: Person("Barack", 55) # Immutable
}
If you want to change something, you need a mutable reference:
fn main() {
var charlie: Person("Charlie", 33) # Immutable
var hillary: Person("Hillary", 68) # Immutable
var barack: Person("Barack", 55) # Immutable
with (&charlie! as p) {
p.age++
}
}
But the core purpose of any program is to mutate memory, and wrapping each
mutation in a with
block is intolerably tedious. Therefore, mutations will
create an implicit mutable reference and use it:
fn main() {
var charlie: Person("Charlie", 33) # Immutable
var hillary: Person("Hillary", 68) # Immutable
var barack: Person("Barack", 55) # Immutable
charlie.age++ # Really looks like (&charlie!).age++
}
Opting in to mutability instead of opting out provides numerous safety guarantees and opens up several avenues for optimization, and combined with references it's easy to manage as well.