(no title)
mojuba
|
2 months ago
I like how Swift solved this: there's a more universal `defer { ... }` block that's executed at the end of a given scope no matter what, and after the `return` statement is evaluated if it's a function scope. As such it has multiple uses, not just for `try ... finally`.
Someone|2 months ago
Defer has two advantages over try…finally: firstly, it doesn’t introduce a nesting level.
Secondly, if you write
, when scanning the code, it’s easier to verify that you didn’t forget the revert_foo part than when there are many lines between foo and the finally block that calls revert_foo.A disadvantage is that defer breaks the “statements are logically executed in source code order” convention. I think that’s more than worth it, though.
Snarwin|2 months ago
https://jacobfilipp.com/DrDobbs/articles/CUJ/2000/cexp1812/a...
A similar macro later (2006) made its way into Boost as BOOST_SCOPE_EXIT:
https://www.boost.org/doc/libs/latest/libs/scope_exit/doc/ht...
I can't say for sure whether Go's creators took inspiration from these, but it wouldn't be surprising if they did.
mojuba|2 months ago
kibwen|2 months ago
dwattttt|2 months ago
https://docs.rs/defer-rs/latest/defer_rs/
unknown|2 months ago
[deleted]
mojuba|2 months ago
troglo-byte|2 months ago
rezonant|2 months ago
pelorat|2 months ago
chuckadams|2 months ago
unknown|2 months ago
[deleted]