5 ms·
Memory isn't the only resource that needs management. File handles, network sockets, locks, security codes and more, all need to have deterministic lifetimes. I
by snovv_crash 4y ago
Memory isn't the only resource that needs management. File handles, network sockets, locks, security codes and more, all need to have deterministic lifetimes. If you language doesn't support lifetime management via some mechanism then all of these things are the next part that will bite, even if you have GC for memory.
- galangalalgol 4y agoThis gets missed a lot. Rust isn't memory safe, it is resource safe.
- hoseja 4y agoIsn't it the other way around?
- vlovich123 4y agoHow so?
- galangalalgol 4y agoMy point is that it is both. The borrow-checker protects every resource, not just memory.
- snovv_crash 4y agoIt's funny that people are assuming I was talking about Rust when I was actually referring to modern C++. RAII is a powerful concept.
- galangalalgol 4y agoIt is, I've written c++ as my primary language for 30 years. I skipped c++14 and I'm still using 17, but staying up to date other than that. But RAII doesn't come anywhere close to the borrow checker in the number of mistakes it prevents.
- pjmlp 4y agoSystem languages with GC like D (one example among many others) support RAII for resource management.
- SigmundA 4y agoIn .Net/C# this is done with IDisposable and the using statement which works quite well. Ex: using (var sr = new StreamReader(filename)) { txt = sr.ReadToEnd(); } https://learn.microsoft.com/en-us/dotnet/api/system.idisposable?view=net-7.0 https://learn.microsoft.com/en-us/dotnet/api/system.idisposa...
- pjmlp 4y agoModern C# version, using var sr = new StreamReader(filename); txt = sr.ReadToEnd(); And for the "what about forgeting to call using?", https://devblogs.microsoft.com/dotnet/infer-interprocedural-memory-safety-analysis-for-c/ https://devblogs.microsoft.com/dotnet/infer-interprocedural-... No different from using all those static analisers in C and C++ to keep the code free of memory corruptions and UB issues.
- MaxBarraclough 4y agoDoes it behave identically without the braces?
- kaba0 4y agoWhile I do love Rust-style resource management, Java’s try-with-resources blocks plus cleaners are not particularly worse.
- amai 4y agoCould a Java version of the future maybe also manage memory with a try-with-resources block ?
- flanked-evergl 4y agoJava and the JVM has a garbage collector, and will likely always have, and GC makes memory management a lot simpler and safer, so while you could use try-with-resources for some kind of memory related resource, it likely would not be to make things safer, but to make things less safe and faster, and if you start doing that you will have significantly reduced safety compared to rust I think.
- kaba0 4y agoYour wish is granted :D https://openjdk.org/jeps/424 https://openjdk.org/jeps/424 Though there was nothing inherent to ever block you from doing it (just create an object that implements Closeable, and malloc in the constructor, free in the close method), this new API makes it quite great. The basic API gives you a SegmentScope which denotes a lifetime, and MemorySegments that point to a memory region (pointer+size+layout+scope). MemorySegments have both spatial and temporal safety, so accessing memory out of boundary will only throw an exception instead of corrupting memory, while any access outside the lifetime of the scope is forbidden. Oh, and they are also thread-safe, only optionally being shared between threads. So in practice it you can write something like try (var arena = Arena.openConfined()) { MemorySegment segment = arena.allocate(someLayout); // use segment } // memory will be deallocated here
- amai 4y agoNice. Thank you for pointing that out.
- 4y ago