• tias@discuss.tchncs.de
      link
      fedilink
      arrow-up
      9
      ·
      15 hours ago

      You can’t, not in e.g. Java or C#.

      C++ and Rust are the only languages I know of that do this right.

      • calcopiritus@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        10 minutes ago

        You can do some kind of RAII in python with the with keyword. Only works for local variables though, where it’s easy to free resources manually

        • tias@discuss.tchncs.de
          link
          fedilink
          arrow-up
          1
          ·
          6 minutes ago

          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.

        • tias@discuss.tchncs.de
          link
          fedilink
          arrow-up
          4
          ·
          11 hours ago

          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.