3 ms·
I wonder if "conditionally send a local variable over a channel" is another good example of something that's hard to clean up with defer. One upside of destruc
by oconnor663 5y ago
I wonder if "conditionally send a local variable over a channel" is another good example of something that's hard to clean up with defer.
One upside of destructors is that adding destructor cleanup to a type isn't necessarily a breaking change. (In Rust terms, as long as the type isn't "Copy".) But in languages with defer or with/using statements, it is.
Another upside of destructors is that the cleaned-up state can be mostly hidden from other code. But when cleanup is a regular function, it's easier to access an object after cleanup, and you have to reason more about what happens in that case. (A Rust example here might be "What happens if you use a Box after it's freed?" The answer is that you just can't do that, even though Box doesn't have a freed/null state.)