Child groups That's a useful rule, and it can get us pretty far. But let's make it even more specific, so we can prove more programs memory-safe. For example, look at this snippet: ref hp_ref = d.hp # Ref to contents damage = a.calculate_damage(d) a_energy_cost = a.calculate_attack_cost(d) d_energy_cost = d.calculate_defend_cost(a) a.use_energy(a_energy_cost) d.use_energy(d_energy_cost) d.damage(damage) print(hp_ref) # Valid! The previous (invalid) program had a ring_ref referring to an element in a ring array. This new (correct) program has an hp_ref that's pointing to a mere integer instead. This is actually safe, and the compiler should correctly accept this. After all, since none of these methods can delete an Entity, then they can't delete its contained hp integer. Good news, Nick's approach takes that into account! But wait, how? Wouldn't that violate our rule? We might have used a reference (damage may have used d) to mutate an object (the Entity that d is pointing to). So why didn't we invalidate all references to the Entity's contents, like that hp_ref? So, at long last, let's relax our rule, and replace it with something more precise. Old rule: When you might have used a reference to mutate an object, don't invalidate any other references to the object's group, but do invalidate any references to its contents. Better rule: When you might have used a reference to mutate an object, don't invalidate any other references to the object's group, but do invalidate any references to anything in its contents that might have been destroyed. Or, to have more precise terms: Even better rule: When you might have used a reference to mutate an object, don't invalidate any other references to the object's group, but do invalidate any references to its "child groups". So what's a "child group", and how is it different from the "contents" from the old rule? If Entity was defined like this: struct Entity: var hp: Int var rings: ArrayList[Ring] var armor: Box[IArmor] # An own...
First seen: 2025-08-28 13:29
Last seen: 2025-08-28 22:30