2 ms·
> The solution you proposed - integers I explicitly said that I don't think integers are the solution; it's simply that for tasks like e.g. managing the thorou
by kibwen 8y ago
> The solution you proposed - integers
I explicitly said that I don't think integers are the solution; it's simply that for tasks like e.g. managing the thoroughly interconnected world state of a video game, where you have a web of dynamic entities, references aren't the right solution, and so people start to concoct other solutions using the primitives they have available. I mention that I think generational indices are the solution (or at least a solution).
> But if you do it with indices into another data structure, then you do, in fact, just work around the borrow checker
Why is it "working around the borrow checker" when it happens in Rust, but "employing an ECS architecture" when it happens in C++? The fundamental insight is that pointers and references as we know them kinda sorta suck at managing arbitrarily-interconnected webs of entities with dynamic lifetime; a garbage collector neatly solves this use case (dynamic lifetimes are their entire jam), but if you're using Rust or C++ then we're assuming that you have stricter performance requirements than a typical garbage collector provides (and if you don't, then you should consider using a GC'd language). An ECS-like approach using indices (which, if you squint and turn your head, resembles a very specialized form of garbage collection) is a way to keep entity management tractable without going all-in on GC.
> now you also have to deal with indices that reference non-existent elements, or (depending on your usage patterns) indices that were not properly updated when an element was deleted from the middle etc.
Generational indices address these problems. Again, I don't think anyone should use integers directly, you should seek to leverage abstractions that other people have already sat down and thought about.
- int_19h 8y agoI don't see how regular pointers don't solve the problem with non-tree-like data structures in general. In C++, for the most part, for a cyclical or back-linking data structure, you'd just do that, and then have a single "real owner" of the whole thing that knows how tear it down; and that one gets an owning reference. The class of problems that can't be solved in this manner is substantially smaller than the class of problem that can't be solved with tree-like structures only. Anyway, the point still stands: this is bypassing the borrow checker, because something cannot be adequately handled by it. And by doing that, you're back to square one with all the problems that the borrow checker was supposed to solve.