Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd an else clause to the foreach keyword #222
Conversation
|
@kvprasoon Can you please share sample code that demonstrates the problem you're trying to solve on PowerShell 6 or later? The closer you can provide to a real-world (non-contrived) example where this would be beneficial, the better. Unless I'm mistaken, the bulk of this problem was resolved long ago (I forget in which version), and modern versions of PowerShell no longer process the internals of a foreach block when the collection that is being enumerated is |
|
Thanks for the comment @KirkMunro .
Yep and agreeing on that. This RFC is actually a feature request to add the Proposal 1 is from my experience as I've hit sometimes at work when an expected collection(supposed to get created) becomes empty and the subsequent |
|
@kvprasoon I understand now. Ok, in that case, you might want to reword this RFC. For example, simply state the intent of the RFC (to add an Also, the way it is worded right now it sounds (to me at least) like this could introduce a breaking change, but that is not the case. That sentence could be removed or reworded. |
|
Thanks, not a Native English speaker :-), I've refined it and added one more proposal. |
|
Now that I've re-read this, and now that I've read about python's For example, instead of this: foreach ($i in $collection) {
...
} else {
# Handle empty collection
}If I really wanted to handle an empty collection in a specific way, I would do this instead: if (-not $collection) {
# The collection was empty; write error and return
Write-Error 'The collection was empty'
return
}
# At this point I expect things to be in working order
foreach ($i in $collection) {
....
}That avoids wrapping and unnecessary indentation, while focusing on handling the error scenarios up front when I can. Also, regarding the alternate proposal, having an Lastly, I believe the current guidance (that will be shared in documentation soon that is being worked on) is for language improvement ideas to be shared in issues in the PowerShell repository, discussed there, and then RFCs can come from those discussions once things have been hashed out a bit. |
fowl2
commented
Aug 28, 2019
|
|
|
@fowl2 Can you explain the point that you're trying to make in more detail please? |
fowl2
commented
Aug 28, 2019
•
|
Some examples: Naive option:
Cons:
Buffering option:
Cons:
Flag variable option:
Pros: works today
Pros: shortest, none of the drawbacks of previous options |
|
Except the |
|
Yeah, if you're using But I still think there's value in the |
I agree there's value in If this does move forward, it would make sense to add |
|
Yep, absolutely, I'd say if we do this we should have this for all our loops. The only exception is probably our |
SeeminglyScience
commented
Aug 28, 2019
•
Depends what the source is. Most using namespace System.Linq
$enumerable = [Enumerable]::Select(
[int[]](0..2),
[Func[int, int]]{
param($x) end {
Write-Host "Processing $x"
Start-Sleep 1
return $x * 2
}
})
# This is actually always false, since PS can't check it's length effeciently.
if (-not $enumerable) {
return
}
# The select delegate is invoked every iteration.
foreach ($item in $enumerable) {}This raises another question. Since PS can't easily tell if the enumerable is empty, how does |
This comment was marked as off-topic.
This comment was marked as off-topic.
|
That's a good question there, Patrick... I think in the case of This would allow folks to work better with less coherent collections and would overall be a benefit, I think? |
kvprasoon commentedAug 22, 2019
No description provided.