-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Guards on comprehensions #17097
Guards on comprehensions #17097
Conversation
…r move Jeff. [ci skip]
Doesn't the |
I think the consensus in the issue was that before the for loop was clearer and would be the only version allowed. |
I vaguely remember that conversation. Just curious if there is precedent for the guard before the for? Not that there has to be, I've just never seen it. |
(Looking at #550 again, I don't dislike the if before the for...) |
I wouldn't call it a consensus yet. I would love to have this, but with the
|
For me the most convincing argument is that this is how Python does it so it will cause the least confusion for people switching languages. |
Personally I've always found the python syntax very disruptive when reading code. You read this entire comprehension/generator and then get surprised at the end by a guard. You then have to go back and re-read everything to make sure you understand what's going on. I mean, there's a reason we don't have begin
do_this()
do_that()
end if surprise_condition Putting right before the iterator also makes it more obvious IMO that its operating on the iterator. |
A weak +1 for putting |
FWIW, Haskell does it like Python too (or Python like Haskell?) |
Either approach is fully general, just a matter of whether the loops and guards appear inside-out or outside-in. The fact that the expression comes first does seem to imply that the whole thing should be inside-out. However I find the precedent from multiple languages slightly more convincing. Reversing the order of something tends to be mind-bending. |
At least for me, the precedent from other languages doesn't mean much since Julia is something new and is in a position to set precedent. As such, I don't find "we should do this because Python/Ruby/etc. does this" very convincing. We have the opportunity to get it right where other languages got it wrong. 😉 |
I mean I do generally agree, but you have to pick and choose your battles. |
.
I find these arguments convincing only when viewing generators/comprehensions as syntactic sugar for an imperative construct, whereas generators/comprehensions generally strike me as a declarative construct mirroring standard mathematical set notation. For example, Additionally, if one views generators/comprehensions as syntactic sugar for an imperative construct and simultaneously finds order-reversal mind-bending, doesn't consistency demand the syntax |
The only issue to me is that reversing the order relative to precedent seems more like a spurious difference than correcting an obviously-wrong-in-hindsight historical decision. I'm ok with the python/haskell order. |
That's fair. I'd still love to see |
Merged into #16622 |
Ref: #550
Note this is only tests and documentation.
👉 @JeffBezanson