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
Can we allow compound statements (loops and conditionals) as the first pipeline segment and as sub-expressions too? #6817
Comments
This kind of begs the question of why can't we use these anywhere in the pipeline directly? It would effectively remove the need for things like |
It makes sense to limit use of expressions to the first pipeline segment, because while expressions create output, they are not prepared to handle pipeline input. In the world of expressions you already have PS> (1..3).ForEach({ $_ + 1 })
2
3
4 An RFC proposal (of mine) suggests surfacing these methods as bona fide PowerShell operators, which would allow you to write: PS> 1..3 -foreach { $_ + 1 }
2
3
4 |
In #10967 (comment), @rjmholt has provided the explanation for why what the initial post is asking for is not possible with the current grammar :
In short, if I understand correctly:
Currently, never the twain shall meet, sadly.
Asking largely hypothetically, @rjmholt - I do recognize how such a change would be the mother of all changes:
In other words: Would a grammar such as the following work (adapted from #10967 (comment))?
|
Yeah if you're starting from scratch, pretty much anything is feasible.
All third party AST based tooling for one. The shape of all of those API's would be dramatically different. As for PowerShell scripts specifically, also yeah. I mean it's probably possible to redesign the language and rewrite the parser without breaking anything, but it'd be one a hell of a trick. |
Thanks, @SeeminglyScience. By contrast, in the context of introducing |
@mklement0 I'm not sure what you're asking for in that issue, from a technical standpoint. How do you propose that be done without making I think you'd have to make them pipelines or expressions, which would also be a breaking change for tooling. Maybe you could introduce new ASTs that are the expression/pipeline version of those statements but that makes it very confusing imo. On a more subjective note, I also just don't like how hard it would be to read. I know PowerShell has historically been all about letting users shoot themselves in the foot, and that can be great. However, after the whole question mark in variable names situation I'm not keen on the idea of adding more features like that. |
Note that shooting yourself in the foot is already possible; that is, # This happily exits your session, irrespective of what comes after the `&&`
exit 0 && Get-Item /foo That the - perfectly sensible and common - reversal does not work is why I'm proposing the exception: # Doesn't work - requires $(...) around `exit 0`; ditto with `return`
Get-Item /foo && exit 0
It is the latter I was thinking of, but I'm definitely out of my depth here.
It may make the implementation more confusing, of necessity (too late to change the fundamentals), but to me it definitely lessens the confusion for the users.
I think that my proposal helps with both aspects, because in the current implementation:
Note that in the context of PowerShell there is no behavioral precedent to adhere to in these situations: These are new features, and the behavior I'm proposing will not only make it easier for Bash users to use them, but, I believe, generally makes more sense and is easier to conceptualize than the current implementation. |
I'm not following, why only
Okay but how do you expect that to work? Because if you change $myInvalidExitCode = 0 && Get-Item /foo
exit $myInvalidExitCode
# or
exit $(0 && Get-Item /foo)
# also, this exits with exit code 30
exit 0 && $(exit 30) And that's kind of the point, the problem isn't "how would you make it work for later portions of the chain" it's how would you fit it into an actual link, changing the behavior significantly.
I hear ya, but I don't agree. I think it makes the language a lot more confusing past the surface level, and any surface level benefit doesn't outweigh the cost of the complexity.
Yeah I agree that it's unfortunate. I still don't think it makes sense for PowerShell.
They are extensions of pipelines which currently follow the same rules.
Probably right in regards to bash users, which is very regrettable. I don't agree with the rest though. My personal opinion is that it should stay how it is. I understand the arguments on both sides fully, I just don't agree on this one. |
My thought was: If we change the With the
In that vein: I expect That As an aside: that Scoping As stated, conditionally exiting (returning from) the current scope based on individual pipelines' outcomes is a primary use case for My hunch is that most PowerShell users will not only naturally assume that
To me, ensuring that a feature makes sense to users is much more than a surface-level benefit.
I don't think of them as extensions to pipelines, but as a conditional sequencing of them; that is, independent pipelines are combined. Other than being forced by historical design decisions ("The current grammar just substitutes pipeline chains for pipelines" - #10967 (comment)), I see no justification for the current behavior. That said, it may well be that what I'm asking for is still too much to shoehorn into / breaks the current grammar - a question I cannot answer by myself:
|
And inconsistent behavior with all other keywords, and with how all other language elements work with
🤷♂ that's how everything else works. I get the confusion in reference to other languages, but it just doesn't make sense for PowerShell imo.
Yeah I'm not disputing that. When I say that I don't think it makes sense for PowerShell, and that it is not feasible, I'm saying that with this in mind. I understand all of the reasons why on the surface this seems like the obvious right move, but I strongly disagree that it is. |
Before And that wouldn't go away with what I'm proposing. No end user previously had to think about the fact that sticking a From an end user's perspective, conceiving of a pipeline chain as a sequence of pipelines each optionally preceded by And I don't think the proposed behavior contravenes the spirit of PowerShell in any way. It does contravene the current implementation, however, and I get that how chaining was implemented fits in best with that.
Not if you think of
While I definitely wish we could also use compound statements such as I can also see how implementing just the But it is clear to me that the current chain implementation was dictated by the limitations of the original grammar - whose subtleties most users are probably unaware of - not by what would make the feature most useful to end users. |
To bring closure to the question asked in the OP, based on @rjmholt's feedback in #10967 (comment): |
I've summarized my conclusions from this exchange with respect to P.S.: Just noticed the thumbs-down on my previous comment, @SeeminglyScience: I get not wanting to expend more energy on a discussion at a certain point (when you feel like the disagreement is understood but no shared understanding can be reached, when you feel like not being heard, when the conversation is going around in circles, ...), but a thumbs-down as the sole feedback on a comment were multiple points were argued in detail just tells us "I don't like this" - and nothing else; it is a gesture of opposition without content. I value your expertise, especially in areas where my knowledge is superficial, but the overall tone of this exchange left a sour aftertaste. |
Sorry, I didn't mean any disrespect. Thumbs down is less common here than in other repos, so it might have a connotation that I didn't intend. That said, it's often stated that decisions are largely made based on community consensus. My intent was to indicate that I disagree without bumping the thread or paraphrasing the reasons why. You could argue that it was clear I disagreed from context, but to be honest it seems like context is often lost in committee meetings. |
In the world of assignments, compound statements (loops and conditionals) can be used as value-returning expressions too:
By contrast, in the context of a pipeline or as sub-expressions, flow-control statements do not work as-is:
You can work around this:
& { ... }
/. { ... }
for streaming output.$(...)
or@(...)
, which collects all output up front.However, the need for that is not obvious, and it is somewhat cumbersome, and in the case of sub-expressions carries a performance penalty.
There may be parsing challenges and ambiguities I'm not considering, but perhaps compound statements can be treated the same as simple expressions in these contexts, allowing their direct use as the first pipeline segment / redirection source.
In other words: If compound statements were bona fide expressions, the above problems would go away (though streaming behavior should be retained when used as the 1st pipeline segment).
P.S.:
Environment data
Written as of:
PowerShell Core v6.0.2
The text was updated successfully, but these errors were encountered: