-
-
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
Super-fancy refactor of Process::Status #9064
Conversation
Currently the value is arbitrary for signal exits, but usually 0, which is horrible if people only check `exit_code == 0`.
Backwards-compatible!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, but this is a no from me (per #8381 (comment)).
Establishing POSIXisms in the Process::Status
API seems counterproductive to our efforts to create a platform-independent interface.
The features look nice, but that's too specific for stdlib. It would probably be a good fit for a posix
/system
shard, though 👍
The POSIXisms are very well established as is, though. If you pretend that those other states don't exist, that's fine too. |
I'm sure we can expose the platform-specific exit value and use that for such specific implementations. |
Could we have a single |
I'm not particularly excited about a struct with 6 members to store 1 number. Which would also do nothing to prevent invalid states. This is also the most straightforward way to implement this, even if you :nodoc: the underlying types. You can try to write out the code, there will be more of it than here. Although my vision is that the types would be used more directly, with benefits of exhaustive |
Those benefits don't apply here because this is a type hierarchy and anyone could extend |
Should this really be an argument? There are easier ways than that to break stdlib if one does such obviously unintended things. Also maybe it's nice for when Linux adds more possible meanings to the range of exit statuses. 😂
|
I'm just saying that we can't prove exhaustiveness if the type is open for everyone to open. We can't just crash the program when we reach the What are the problems with Ruby: https://ruby-doc.org/core-2.5.1/Process/Status.html ? |
Well, the API is cluttered and every method's return value is nilable. It's not the worst solution, though, and we're just minor tweaks (though quite a few added methods) away from that. It just means that the logic can't move into Just that people have been suggesting refactors where we don't store the exit_status. And I found a very nice way to achieve that, while also potentially decreasing the number of methods that programmers are exposed to by default. |
Can we make each method return a nilable something that has all the properties if it's there, but Otherwise, it might be fine to return a union type... as long as they all quack similarly for the common cases ( |
Mm I guess it's about time for me to share the intended usage. Basic: if !status.success?
puts "Oops, code #{status.exit_code}" # can be a negative number for signals, which is not great
end Advanced: case status
when Process::Status::Exited
puts "Exited with code #{status.exit_code}"
when Process::Status::Signaled
puts "Terminated with signal #{status.signal}"
puts "Core dumped" if status.core_dumped?
else # mandatory!
puts "Something wrong, don't care!"
end Alternate (per previous comment's suggestion, and already works, though ideally I'd remove it): if (signaled = status.signal_exit?)
puts "Terminated with signal #{signaled.signal}"
else
puts "Uh probably exited normally? No idea"
end Regarding the last part, this is what can happen frequently in the current state - people want to check all outcomes but don't realize that normal exit and signal exit are not the only possible states. crystal/src/compiler/crystal/compiler.cr Line 579 in f85bdb2
|
Mm I just realized that Yeah now this is overkill. |
This uses a hierarchy of structs; it's a type-safe approach to the variety of POSIX exit statuses.
Based on #8381 (comment), #8381 (comment)
Usage examples
It is also backwards-compatible
I don't know how this will be received, but it doesn't matter, I had to put this out there 😂