But if you ever forget the with keyword, you have a leak. With proper RAII the resource acquisition and release is a property of the object’s type and impossible to forget.
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.
You can’t, not in e.g. Java or C#.
C++ and Rust are the only languages I know of that do this right.
You can do some kind of RAII in python with the
withkeyword. Only works for local variables though, where it’s easy to free resources manuallyBut if you ever forget the
withkeyword, you have a leak. With proper RAII the resource acquisition and release is a property of the object’s type and impossible to forget.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.