3 ms·
> just a hack to get around the borrow checker as people like Jonathan Blow have famously critiqued [2], it may just be the right way of doing things. I dunno,
by IshKebab 3y ago
> just a hack to get around the borrow checker as people like Jonathan Blow have famously critiqued [2], it may just be the right way of doing things.
I dunno, isn't a usize index into a collection essentially just the same as a pointer? Ok it's a bit safer because you at least know the type of the object you're accessing is correct, but you can still get e.g. use after free bugs.
I think Jonathan Blow is right and wrong - it is a hack to get around the borrow checker, but also that's totally fine. The borrow checker still works for the other 95% of code you are writing. Nobody ever claimed it was the perfect solution to every problem.
- winwang 3y agoDon't you also know the type of the object when dealing with pointers and references (assuming no casts)? Though arenas are useful even outside of "hacks". In the end, a pointer (or a reference, even) is "just" a usize index into the heap, which can be thought of as a heterogenous collection.
- db48x 3y agoYep. A pointer/reference is just an index into a giant shared mutable array of bytes, which is exactly what causes all of the problems :)
- IshKebab 3y agoNo. An invalid pointer can point to literally anything. Just because the pointer type says it points to T doesn't mean it really does. With an index into Vec<T> you are at least guaranteed that the object will be a T. You are guaranteed memory safety, but you aren't guaranteed to have a correct reference.