It’s not impossible, at least in C# I know you can mark objects that need to be RAII with some custom attribute and enforce correct usage patterns with a custom analyzer. You need to write it yourself but it’s not impossible that way.
Maybe you’re thinking of IDisposable with the using keyword. The problem with that is that you have to remember to use it in every call site. Nothing stops a caller from writing var x = new Foo() and walking away.
Seems like someone doesn’t RAII their connections!
You can’t, not in e.g. Java or C#.
C++ and Rust are the only languages I know of that do this right.
It’s not impossible, at least in C# I know you can mark objects that need to be RAII with some custom attribute and enforce correct usage patterns with a custom analyzer. You need to write it yourself but it’s not impossible that way.
Maybe you’re thinking of
IDisposablewith theusingkeyword. The problem with that is that you have to remember to use it in every call site. Nothing stops a caller from writingvar x = new Foo()and walking away.