Wrong by Default

16 · Kevin Cox · May 13, 2022, 3:25 p.m.
A few programming languages use a “defer” pattern for resource cleanup. This is a construct such as a defer keyword which schedules cleanup code to run at the end of the enclosing block (or function). This is available in Zig, Go and even in GCC C.fn printFile(path: str) !void { var f = try open(path) defer f.close() for line in f { print(try line) } // File is closed here. } This provides a few key benefits.The cleanup is right next to the allocation. It is easy to spot and keep in sync....