4 ms·
Wow, errdefer sounds like the kind of thing every language ought to have.
by skrebbel 1y ago
Wow, errdefer sounds like the kind of thing every language ought to have.
- jayd16 1y agoIs it significantly different than a try-catch block?
- ijustlovemath 1y agoone less level of indentation; reading down is happy path, seems nice!
- jayd16 1y agoHuh? But the exceptional behaviors are listed first? Would you say we should move to catch-try syntax? Seems strange to me.
- skrebbel 1y agoYeah it lets you put code that goes in "catch" all over your function body, right next to where it's most relevant. It lets you go "hm but what about errors? ah handled! ok i can forget about it" when reading a function without having to skip back and forth between the main code and the catch block.
- jayd16 1y agoBut that's just try blocks around where it's relevant except the more precise scoping of a block and the catch being after where the errdefer is before.
- masklinn 1y ago> Yeah it lets you put code that goes in "catch" all over your function body, right next to where it's most relevant. To be fair I’ve rarely seen that, usually you’ve got half the method in a giant try block and have fun untangling which errors are caught intentionally and which are side-effects.
- BradleyChatha 1y agoIf I understand the feature correctly, D has this via scope(failure) [1] ``` void func() { scope(failure) writeln("Something went wrong!"); } ``` [1] https://tour.dlang.org/tour/en/gems/scope-guards https://tour.dlang.org/tour/en/gems/scope-guards