Re:Lies we tell ourselves to keep using Golang
by mike_hearn on Wednesday November 27, 2024 @04:30AM
(#42254473)
Haven't seen ScopeGuard before but it looks like an implementation of defer() in C++?
That sort of thing can help yes. But it's still way harder to get exception safety right in a language with manual memory management. In a GCd language you only have to be careful about cleaning up non-GCd resources, whereas in C++ you have to be ready for the expected lifetimes of things to be violated by exception unwinds at many different points and if you get it wrong, you corrupt the heap. Very few C++ codebases use exceptions vs all of them for Java, and I think that's why.
Re:Lies we tell ourselves to keep using Golang
by quotemstr on Wednesday November 27, 2024 @12:38PM
(#42257938)
A significant number of C++ codebases use exceptions. Google famously doesn't, but Meta, alike in dignity, does. GDB, now C++, does. At least some AI labs do. C++ exceptions are normal and common.
In your map example, just add a scope guard that removes the just-added element using the returned iterator if the rest of the procedure doesn't succeed. It's no different in Java.