-
-
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
Use an Enum for process stdio redirections #4445
Use an Enum for process stdio redirections #4445
Conversation
src/process.cr
Outdated
input : Stdio = INHERIT, | ||
output : Stdio = INHERIT, | ||
error : Stdio = INHERIT, | ||
chdir : String? = nil) |
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.
I suggest to inline this method signatures like thoses above and below this one
input : Stdio = INHERIT, output : Stdio = INHERIT, error : Stdio = INHERIT, chdir : String? = nil)
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.
Fixed.
056439c
to
e6ce3f4
Compare
src/process.cr
Outdated
INHERIT | ||
end | ||
|
||
PIPE = Redirect::PIPE |
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.
I think we need documentation on thoses constants too
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.
I guess, but looking at the core/stdlib we don't seem to document enums much, so I'm not sure what I should document here. The copied constants? The enum itself or each enum value?
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.
I add a proposal for naming constant comments above.
I suppose that documentation can be copied, but we can make #See whatever
too.
Or, may be, we can to private PIPE = Redirect::PIPE
?
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.
Yes, private constants are supported, so you'll can set aliases as private.
enum Whatever
One
Two
end
private WH = Whatever::One
p WH
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.
@akzhan using private
will block you from accessing PIPE
from outside Process
namespace.
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.
As per #3084 the aliases are to be used outside of the Process
class, so we don't have to type the lengthy Process::Redirect::CLOSE
but Process::CLOSE
. I wish symbols were transformed automatically to enum values (when an enum is expected), so we could just use :close
.
src/process.cr
Outdated
alias Stdio = Nil | Bool | IO | ||
# How to redirect the standard input, output and error IO of a process. | ||
enum Redirect | ||
PIPE |
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.
# Use a pipe
src/process.cr
Outdated
# How to redirect the standard input, output and error IO of a process. | ||
enum Redirect | ||
PIPE | ||
CLOSE |
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.
# Redirect to /dev/null
src/process.cr
Outdated
enum Redirect | ||
PIPE | ||
CLOSE | ||
INHERIT |
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.
# Inherit from parent process
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.
Anyway we must use Process::Redirect::PIPE
outside of the class, as I suppose.
No Process::PIPE, so PIPE alias should be private.
src/process.cr
Outdated
INHERIT | ||
end | ||
|
||
PIPE = Redirect::PIPE |
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.
Yes, private constants are supported, so you'll can set aliases as private.
enum Whatever
One
Two
end
private WH = Whatever::One
p WH
@akzhan the advantage of defining thoses constants ( The objective is to use them as We could make the enum |
e6ce3f4
to
309f71a
Compare
I added a tiny explanation of enum values. If nobody objects, I'll merge this a bit later. |
That is why module Foo
enum Bar
ONE = 1
TWO = 2
end
ONE = Bar::ONE
private TWO = Bar::TWO
end
p Foo::ONE # => 1
# uncomment the following fails to compile (Error in line 14: private constant Foo::TWO referenced)
# p Foo::TWO |
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.
LGTM!
Is this considered as a breaking change for next release?
Yes. It's a breaking change. For the greater good, I hope. |
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.
LGTM
LGTM too. |
So is this ready for merge? |
src/process.cr
Outdated
enum Redirect | ||
# Pipe the IO so the parent process can read (or write) to the process IO | ||
# throught `#input`, `#output` or `#error`. | ||
PIPE |
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.
Why are the enum constants all caps? Usually enums use PascalCase.
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.
My habit. They're constants, not types, so I always put them as uppercase, not camelcase. I don't mind changing this.
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.
I think there are a lot of other places in the stdlib where the enum constants are pascal case, so this should be changed for consistency.
309f71a
to
caacef2
Compare
I renamed enum constant to camelcase. |
src/process.cr
Outdated
Inherit | ||
end | ||
|
||
PIPE = Redirect::Pipe |
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.
I would have renamed these to also be camel case to match the enum constants. You could argue either way but what does everyone else think?
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.
Yeah, they're constants that happen to be an enum value. It would be confusing to start having camelcase constants —thought there are some camelcase constants (e.g. under Time
) which irritates me.
Hence why I believe all constants, including enum constants, should always be uppercase, while types are always camelcase.
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.
Simple rule, no confusion.
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.
Yeah, where's that camelcase for enum values coming from anyway? :/
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.
Even though they are technically constants, usage of Enum values is closer to symbols (in a sense that their only value is their name), so i prefer them to be camelcase, not SCREAMING on me. But it's a matter of taste of course.
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.
Personally, I'd consider removing these constants, stdin: Process::Redirect::Pipe
is more explanatory to me and looks nicer than stdin: Process::PIPE
. I don't like the inconsistency in case between the enum constants and the constants on Process
.
caacef2
to
543607f
Compare
Changed to use the enum. I really wish symbols were translated to enum values at compile time, because typing |
Just as a quick question, does anyone actually use symbols for a usecase which couldn't reasonably be replaced by an enum? Perhaps it would be easier to remove symbols and use the syntax for the much more common enum shorthand? |
Yup: |
543607f
to
b45d6f0
Compare
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.
Great work!
Implements a
Process:Redirect
enum as per #3084 to define the standard input, output and error IO of spawned processes. This patch replaces the inexplicit and confusingnil
,true
andfalse
values for the explicitness ofPIPE
,CLOSE
andINHERIT
.closes #3084