Try Unsafe block #3109
Replies: 4 comments
-
Generally speaking, when you're dealing with pointers and other native resources within the context of a try block, wouldn't you also need to access those same pointers / resources in a finally block? So you'd almost certainly need the pattern In your particular example the use of |
Beta Was this translation helpful? Give feedback.
-
Why does this need a language change at all? You can already avoid the extra indentation by putting the |
Beta Was this translation helpful? Give feedback.
-
The syntax I think edit: checked statement has an expression counterpart, so it'll be probably ambiguous. |
Beta Was this translation helpful? Give feedback.
-
Just wanted to point out that this isn't possible in async contexts and you explicitly have to add |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Similarly to how we can stack
using
blocks, I would like to see us be able to stacktry
andunsafe
.E.g.:
Use-case
Sometimes, especially in the case of try-finally blocks, the entire try clause is unsafe context. For instance, here's some code I put together in learning how to use
Span
:In this case, the marshal operation could be placed in the unsafe block without issue, and thus the entire try clause could be made unsafe. (Forgive me for using SecureString, the point was learning how to use low-level stuff like marshaling and span, so I went all-in. This is not production code).
I felt
try unsafe
should be the proper order since it reads somewhat like "try [this] unsafe [action]," though ifunsafe try
seems better due to keyword ranking or whatever other reasons, I'd love to hear thoughts.Beta Was this translation helpful? Give feedback.
All reactions