Shape-Stability
Ante has a concept of shape-stability, which means "a reference to something of stable shape is always valid 6 no matter what mutations are made elsewhere."
Because of this, Ante code can safely have multiple mutable borrow references to the same struct at the same time.
To set the stage, here's a heal function taking two mutable references to Entitys:
type Entity =
energy: I32
health: I32
heal (healer: mut Entity) (target: mut Entity) =
healer.energy -= 10
target.health += 10
In Ante, we can call heal with the same Entity for both parameters — for example, when an entity heals itself:
self_heal (entity: mut Entity) =
heal entity entity
Mutating healer can't invalidate shared references to the Entity in any way, 7 so the compiler accepts this code as valid.
In other words, even though healer and target might point to the same Entity, this is memory safe: nothing here can destroy the Entity, so both references stay valid.
Now let's get a little more complicated.
Ante code can actually have multiple mutable borrow references to the same struct, or any of its struct fields, or any of their struct fields, at the same time.
For example, here we have a mutable borrow reference pointing at the ship, and another mutable borrow reference pointing at the ship's engine, at the same time.
type Engine =
fuel: I32
type Spaceship =
engine: Engine
name: String
refuel (ship: mut Spaceship) =
engine_alias: mut Engine = ship.engine
ship.engine.fuel := 200
engine_alias.fuel := 100
Ante knows this is completely memory safe, because during this function, nobody can destroy ship, and therefore nobody can destroy its engine or its fuel.
In other words, these references are to shape-stable data.
Those familiar with Rust and Swift will be surprised by this:
Now let's add some reference counting into the mix!