{"action":"created","issue":{"url":"https://api.github.com/repos/ziglang/zig/issues/782","repository_url":"https://api.github.com/repos/ziglang/zig","labels_url":"https://api.github.com/repos/ziglang/zig/issues/782/labels{/name}","comments_url":"https://api.github.com/repos/ziglang/zig/issues/782/comments","events_url":"https://api.github.com/repos/ziglang/zig/issues/782/events","html_url":"https://github.com/ziglang/zig/issues/782","id":299192557,"node_id":"MDU6SXNzdWUyOTkxOTI1NTc=","number":782,"title":"automatic resource deallocation and enforced resource handling","user":{"login":"andrewrk","id":106511,"node_id":"MDQ6VXNlcjEwNjUxMQ==","avatar_url":"https://avatars0.githubusercontent.com/u/106511?v=4","gravatar_id":"","url":"https://api.github.com/users/andrewrk","html_url":"https://github.com/andrewrk","followers_url":"https://api.github.com/users/andrewrk/followers","following_url":"https://api.github.com/users/andrewrk/following{/other_user}","gists_url":"https://api.github.com/users/andrewrk/gists{/gist_id}","starred_url":"https://api.github.com/users/andrewrk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andrewrk/subscriptions","organizations_url":"https://api.github.com/users/andrewrk/orgs","repos_url":"https://api.github.com/users/andrewrk/repos","events_url":"https://api.github.com/users/andrewrk/events{/privacy}","received_events_url":"https://api.github.com/users/andrewrk/received_events","type":"User","site_admin":false},"labels":[{"id":693800736,"node_id":"MDU6TGFiZWw2OTM4MDA3MzY=","url":"https://api.github.com/repos/ziglang/zig/labels/proposal","name":"proposal","color":"aaefdc","default":false},{"id":693800858,"node_id":"MDU6TGFiZWw2OTM4MDA4NTg=","url":"https://api.github.com/repos/ziglang/zig/labels/rejected","name":"rejected","color":"b60205","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/ziglang/zig/milestones/6","html_url":"https://github.com/ziglang/zig/milestone/6","labels_url":"https://api.github.com/repos/ziglang/zig/milestones/6/labels","id":3149533,"node_id":"MDk6TWlsZXN0b25lMzE0OTUzMw==","number":6,"title":"0.4.0","description":"Accept and resolve these issues, reject, or postpone them before releasing 0.4.0.","creator":{"login":"andrewrk","id":106511,"node_id":"MDQ6VXNlcjEwNjUxMQ==","avatar_url":"https://avatars0.githubusercontent.com/u/106511?v=4","gravatar_id":"","url":"https://api.github.com/users/andrewrk","html_url":"https://github.com/andrewrk","followers_url":"https://api.github.com/users/andrewrk/followers","following_url":"https://api.github.com/users/andrewrk/following{/other_user}","gists_url":"https://api.github.com/users/andrewrk/gists{/gist_id}","starred_url":"https://api.github.com/users/andrewrk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andrewrk/subscriptions","organizations_url":"https://api.github.com/users/andrewrk/orgs","repos_url":"https://api.github.com/users/andrewrk/repos","events_url":"https://api.github.com/users/andrewrk/events{/privacy}","received_events_url":"https://api.github.com/users/andrewrk/received_events","type":"User","site_admin":false},"open_issues":224,"closed_issues":24,"state":"open","created_at":"2018-02-28T05:37:00Z","updated_at":"2018-07-14T01:39:39Z","due_on":null,"closed_at":null},"comments":16,"created_at":"2018-02-22T01:37:58Z","updated_at":"2018-07-14T01:39:57Z","closed_at":"2018-07-14T01:39:39Z","author_association":"MEMBER","body":"This pattern is extremely common in zig code:\r\n\r\n```zig\r\n    const err_pipe = try makePipe();\r\n    errdefer destroyPipe(err_pipe);\r\n\r\n    var in_file = try os.File.openRead(allocator, source_path);\r\n    defer in_file.close();\r\n\r\n    var atomic_file = try AtomicFile.init(allocator, dest_path, mode);\r\n    defer atomic_file.deinit();\r\n\r\n    var direct_allocator = std.heap.DirectAllocator.init();\r\n    defer direct_allocator.deinit();\r\n\r\n    var arena = std.heap.ArenaAllocator.init(&direct_allocator.allocator);\r\n    defer arena.deinit();\r\n```\r\n\r\nGenerally:\r\n\r\n```zig\r\n    const resource = allocateResource();\r\n    defer deallocateResource(resource); // or errdefer\r\n```\r\n\r\nThis proposal is to \r\n * make it harder to forget to clean up a resource\r\n * make it easier to clean up resources\r\n\r\nStrategy:\r\n * Functions which allocate resources are annotated with the corresponding cleanup function.\r\n * New keywords, corresponding to the defer keywords:\r\n   - `clean` corresponding to `defer theCleanupFunction(resource);`\r\n   - `errclean` corresponding to `errdefer theCleanupFunction(resource);`\r\n * If you want to handle resources manually, you must use `noclean` to indicate that you accept\r\n   responsibility for the resource. Otherwise you get `error: must specify resource cleanup strategy`.\r\n\r\nThe above code example becomes:\r\n\r\n```zig\r\n    const err_pipe = errclean try makePipe();\r\n    var in_file = clean try os.File.openRead(allocator, source_path);\r\n    var atomic_file = clean try AtomicFile.init(allocator, dest_path, mode);\r\n    var direct_allocator = clean std.heap.DirectAllocator.init();\r\n    var arena = clean std.heap.ArenaAllocator.init(&direct_allocator.allocator);\r\n```\r\n\r\nHow to annotate cleanup functions:\r\n\r\n```zig\r\n// std.mem.Allocator\r\nfn create(self: &Allocator, comptime T: type) !&T\r\n    cleanup self.destroy(_)\r\n{\r\n    const slice = try self.alloc(T, 1);\r\n    return &slice[0];\r\n}\r\n\r\n// function pointer field of struct\r\nallocFn: fn (self: &Allocator, byte_count: usize, alignment: u29) Error![]u8\r\n            cleanup self.freeFn(self, _),\r\n\r\n// std.os.File\r\npub fn openRead(allocator: &mem.Allocator, path: []const u8) OpenError!File\r\n    cleanup _.close()\r\n```\r\n\r\nHaving functions which allocate a resource mention their cleanup functions will make generated documentation more consistent and helpful."},"comment":{"url":"https://api.github.com/repos/ziglang/zig/issues/comments/404989816","html_url":"https://github.com/ziglang/zig/issues/782#issuecomment-404989816","issue_url":"https://api.github.com/repos/ziglang/zig/issues/782","id":404989816,"node_id":"MDEyOklzc3VlQ29tbWVudDQwNDk4OTgxNg==","user":{"login":"andrewrk","id":106511,"node_id":"MDQ6VXNlcjEwNjUxMQ==","avatar_url":"https://avatars0.githubusercontent.com/u/106511?v=4","gravatar_id":"","url":"https://api.github.com/users/andrewrk","html_url":"https://github.com/andrewrk","followers_url":"https://api.github.com/users/andrewrk/followers","following_url":"https://api.github.com/users/andrewrk/following{/other_user}","gists_url":"https://api.github.com/users/andrewrk/gists{/gist_id}","starred_url":"https://api.github.com/users/andrewrk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andrewrk/subscriptions","organizations_url":"https://api.github.com/users/andrewrk/orgs","repos_url":"https://api.github.com/users/andrewrk/repos","events_url":"https://api.github.com/users/andrewrk/events{/privacy}","received_events_url":"https://api.github.com/users/andrewrk/received_events","type":"User","site_admin":false},"created_at":"2018-07-14T01:39:57Z","updated_at":"2018-07-14T01:39:57Z","author_association":"MEMBER","body":"Too complicated"}}
{"actor":{"display_login":"andrewrk"}}