Simplification of the finally block. #10074
Unanswered
EshG
asked this question in
Language Ideas
Replies: 2 comments 6 replies
-
|
The third code snippet in particular is hard to read. I would want that to stay in the current form, if it was code I had to maintain. |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
While explicit finally blocks are sometimes necessary, many use cases only require cleanup logic at the end of a method's scope. In such cases, resource cleanup is structurally simple, yet it must be expressed using try/finally, which is primarily an exception-handling construct.
This often leads to repetitive try/finally constructs that could be made implicit.
This proposal explores two variations that aim to simplify this pattern while preserving the semantics and guarantees of finally.
There has been prior discussion around try/finally generation in the context of using (for example, a proposal to suppress the generated finally block). That discussion focuses on avoiding finally for performance or safety reasons, whereas this proposal focuses on improving the expressiveness and ergonomics of finally while preserving its guarantees.
This form moves the
finallyoutside the method block, preserving the familiartry { } finallyshape without requiring an explicittryblock around a single statement.Similar to the blockless
usingstatement, this form treatsfinallyas a statement and supports multiple protected statements.Extends the previous example with more complex control flow, illustrating nesting and the grouping behavior of multiple
finallyclauses. The equivalent lowered form is shown below.The above sample is equivalent to:
Beta Was this translation helpful? Give feedback.
All reactions