-
-
Notifications
You must be signed in to change notification settings - Fork 1.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::Mode
flag enum
#11901
Comments
related: #10680 |
How does that work and when? |
If certain combinations of flags are relatively common then they should be defined as constants somewhere. To be honest I find Don't know if anyone mentioned this, but since these flags are not rooted in POSIX we could skip the binary / text mode flags entirely. However, we should still merge #11824 until the string modes are deprecated and removed. |
Good point. They're already not represented in |
@HertzDevil Common combined modes are provided with input from a variety of other languages and discussion in #7857
Some possible additions:
I provided a non exhaustive list of possible file modes without platform specifics in here. Summary: there are tons of variations between open modes. Those listed above and in the PR probably provide 95% use cases with 1 or sometimes 2 flags. Also note the current combined modes make some All platform specific options TBD in future PR's. |
One option that was not mentioned in #7857 was O_CLOEXEC and O_NONBLOCK. Both are sometimes set today using |
For files |
Well, that makes it even more obvious of a question: Would this enum be created only for external APIs or would it be used internally as well? If the latter, then CLOEXEC should be part of it, no? Sockets are also files. Different types of files have different flags that are relevant. |
This issue is Crystal portable public The current flags can't apply to sockets:
In a future PR, flags supported on most platforms that can gracefully degrade if not supported such as:
|
Bump? |
That may apply to the underlying OS implementation. But it's not represented in Crystal's stdlib APIs. |
In order to move this forward, I'd suggest to start with a new PR. It should be as minimal as possible and avoid any additional changes.1 It should focus on only adding @didactic-drunk Do you want to continue with this? Footnotes
|
Yes, but my bandwidth is low. Can't do immediately |
#8011 proposes a
File::Mode
flag enum as an alternative and eventual replacement for the string-based file mode configuration which is inherited fromfopen
.I think this is a good change and it has received positive feedback. But some of the API details may need some ironing out, and I'd rather do that in a dedicated issue than the PR itself, to focus on the implementation there.
The proposed
File::Mode
looks good. Could use some documentation, but the API should be fine.For
File.new
and.open
, I'm not very happy withmode
being a splat parameter. That's essentially a hack to allow easy combination of short names for the flag enum members.:create | :truncate
doesn't work.Maybe that's a good solution, but I'm not convinced about that. I would suggest to exclude the splat parameter for now. It can be just
mode : Mode
for now. At call site it's possible to useFile::Mode.flags(Create, Truncate)
orFile::Mode::Create | File::Mode::Truncate
. The splat parameter is a convenience feature and not required to make the API work. We can still add it later, but I'd like to think a bit more about this. I'd suggest looking into alternatives, such as language support for flag enum construction at autocast (#10680). If it works, it would be a superior and generic solution of the original problem, and remove the need for a hack.The param name should be singular to make sense as a named argument:
File.new(path, mode: :read)
.The text was updated successfully, but these errors were encountered: