5 ms·
Why not inserting a call to free automatically instead of forcing you to do it manually?
by ithkuil 4y ago
Why not inserting a call to free automatically instead of forcing you to do it manually?
- southerntofu 4y agoIsn't that exactly why Rust needs lifetime annotation and Drop trait implementations? How could you achieve the same result without that, or without a garbage collector alternatively?
- shirleyquirk 4y agoYou'd use automatic reference counting, as Swift and Nim do.
- southerntofu 4y agoGood point! I had previously assumed this was considered a form of "garbage collector" although not tracing.
- j-james 4y agoWell, sort of - it adds overhead, though in the form of an extra byte/object and an extra CPU cycle when objects are allocated/deallocated to check/update the reference counts, rather than the lag spikes tracing GCs are known for.
- whateveracct 4y ago1. When you call free matters - it does something after all! 2. You can build abstractions that call free for you. The common withX callback-style function can be better typed with linear types and call free for you, for instance.
- ithkuil 4y ago2 would be a kind of explicit destructors/drop ?
- pjmlp 4y agoSomething like this fictional language, with-some-resource { res -> // use res as you like } The with-some-resource function takes care of the resource management, while the type system takes care res doesn't escape the lambda.
- ithkuil 4y agoWhat if you want to move the ownership of the resource and let the new owner eventually to dispose it?
- whateveracct 4y agoThen withX isn't the right abstraction. The original situation (malloc gives you a linear variable) handles what you want. A function that takes the variable but does not return it effectively "takes ownership of it" and the type system will force a free. There's a "Linear Constraints" paper draft out there that emulates ownership and borrowing with linear types, and uses the titular new language feature to greatly improve ergonomics.