-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
File/Dir deinit invalidates, close doesn't #10213
Conversation
Andrew has stated that Zig requires a way to close and invalidate a File. This PR provides that by adding the "deinit" function to File which will call "close" and then invalidate. At the same time, I've modified Dir to use the same pattern, where deinit will close and invalidate whereas close will only release the OS resource. Because Dir did not do this before, zig code was unable to use "const dir" but can now do so with this change. I've gone through std looking for ".close(" calls and modified the respective Dir or File handles to be const when possible. To me this makes the code more readable because using const reduces the mental load when reading code.
I think in this case
So if anything should invalidate, it's |
Are you saying there's no need for the ability to close a file/dir without invalidation, or are you just concerned about the names of the functions and misleading comment? My proposition is that allowing file/dir instances to be const is the reason for supporting close without validation, so there is a reason, but I can understand if you think that is not a good enough reason? Is that the case or are you just concerned about the function names/comment? |
I'm saying I don't think it's worth having both functions. Having two functions that release resources and leave the object in an unusable state is unnecessary complexity. Personally I also don't think it's worth preserving allowing File and Dir to be used as |
@SpexGuy the purpose of |
Yeah, I agree with that. Maybe with better names, like The readability argument for |
@SpexGuy I'm perfectly happy with I'm also working on the opposite of this proposal, making |
Sounds good, thanks!
I don't necessarily agree with this. Zig is openly not stable yet, especially the std lib. We should schedule breaking changes carefully with the release schedule in mind, but other than that I don't think breaking changes need special consideration. |
Closing in favor of #10214 |
@andrewrk do you mind sharing your thoughts on why you prefer #10214? I've shared some reasons on IRC/Discord why immutable file/directory handles are simpler to work with/understand. Do you not think immutable handles are simpler? Or maybe you agree they are simpler, just not enough to justify the addition of a P.S. Also if we are giving up immutability for the sake of invalidation, should be be doing this for everything? For example, should var slice = allocator.alloc(u8, 100);
defer allocator.free(&slice); |
Andrew has stated that Zig requires a way to close and invalidate a File. These are my proposed changes to address that without requiring all
File
instances to be mutable. This PR provides that by adding the "deinit" function to File which will call "close" and then invalidate. At the same time, I've modified Dir to use the same pattern, where deinit will close and invalidate whereas close will only release the OS resource. Because Dir did not do this before, zig code was unable to use "const dir" but can now do so with this change. I've gone through std looking for ".close(" calls and modified the respective Dir or File handles to be const when possible. I also found a few places where tests were using mutable files, in those cases I modified them to calldeinit
to invalidate the file. To me this makes the code more readable because using const reduces the mental load when reading code. When I seevar dir
orvar file
my brain makes a mental note that the code can modify the file handle, but withconst
I know it won't.