I agree it's also an argument for defer being able to consume noncopyable values, and I'd like to see that happen. But even so, it wouldn't cover every case. For example:
func openDiskFile(_ path: FilePath) throws -> Win32.FileHandle {
let handle = try Win32.FileHandle.open(path, access: .readData)
guard try handle.fileType() == .disk else { throw NotADiskFile() }
return handle
}
A consuming defer still wouldn't work here since return handle already consumes the handle (unless defer skipped values that were already consumed, which is what a closing deinit already does). So if we wanted to force a client not to rely on deinit, e.g. via trapping, they would still need to write:
let handle = try Win32.FileHandle.open(path, access: .readData)
do {
guard try handle.fileType() == .disk else { throw NotADiskFile() }
} catch {
try? handle.close()
throw error
}
return handle
which is not so ergonomic. In cases where a consuming defer would work, defer { try? handle.close() } is exactly the same close as the deinit, just with extra code. An application that wants every close written out like this can still opt in with a wrapper whose deinit traps.