This issue doesn't really bother me, I just use `let x = block:` if I want to
compute-then-freeze a variable.
But I do have a similar issue which I wish I could solve:
proc update(self: ptr Entity) =
self.doStuff()
if self.health <= 0.0:
destroyEntity(self) # this invalidates self, so we're not
supposed to use it anymore!
playSound(sfxEnemyDied)
createParticles(self) # oops, we used self by mistake.
else:
self.health += 0.1 # this is fine, because we know self can't have
been destroyed.
self.pos.x += 1 # this is bad, because self _may_ have been
destroyed.
Run
Basically what I want is to guarantee that a variable is only accessed on code
paths where it's known to be in a valid state, i.e. a means of flagging a
variable to make it impossible to use unless the compiler knows for a fact that
it wasn't destroyed.
I suppose [not
nil](https://nim-lang.org/docs/manual_experimental.html#strict-not-nil-checking-not-nil)
would allow me to do this. I could shadow `self` to make it nilable, then have
it so that `destroyEntity` takes a nilable `var ptr` and sets it to `nil`, and
now the compiler will detect both of the bad cases from above.