-
-
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
break with value + loop else clauses #22891
Comments
This commit enables a combination of two language features that dovetail nicely together (as remarked by @StefanKarpinski in [1]): for/else and break-with-value, which together allow something like: name = for person in people if person.id == id break person.name end else generate_name() end The parsing patch is pretty straightforward. As for the code lowering part, I opted to make a second version of `'break-block`, called `'break-block-with-value`, which passes a target variable for the return value onto the `break-labels` stack. That's where the `'break` operation finds it. An alternative approach would be something more similar to the existing `replace-return` function: Instead of having an intermediate node representing `'break-block-with-value`,we could traverse the expression tree and replace `break-with-value` by an assignment followed by a break. I have no opinion either way; the current commit seemed like the obvious implementation to me, but that was before I saw `replace-return`'s prior art. This patch still has a few places marked `TODO`, where scoping issues for the `else` block need to be resolved. I'll tackle that after awaiting feedback. [1] JuliaLang#22891
I took a jab at this with the pull request mentioned above; I took it as an opportunity to learn a bit about Julia's internals. Let me know what you think! I'm not particularly attached to the current implementation; I'll be happy to learn from any suggestions or alternative patches. |
Since this issue is still open but most of the discussion appears to occur in #23260, I posted some ideas #23260 (comment) for if this is ever taken up again. |
Once upon a time, I proposed giving a meaning to
for-else
andwhile-else
that would be quite at odds with the meaning given to these constructs in Python. In version 1.19, Rust introduced a different feature which finally made Python'sfor-else
andwhile-else
contructs make sense to me:break
with a value. Of course, Python still doesn't have break with a value and Rust doesn't havefor-else
orwhile-else
, but these features really dovetail nicely. Let me explain by showing how this would work in Julia...Loops in Julia are expressions like everything, but they evaluate to
nothing
. If we addedbreak
with a value, then a loop that's exited withbreak x
would evaluate tox
, allowing one to write something like this:After this executes,
p
is equal to the first number invalues
that's prime (of course, we could usefindfirst
, but bear with me). This code is type-unstable, however: ifvalues
happens to contain no primes, then the loop evaluates tonothing
. Enter the Python-styleelse
clause on afor
loop:Now
p
is set to the first prime occurring invalues
, or it is set to the maximum value ofvalues
. This is a silly example, but you get the point. This is all non-breaking since all of these syntaxes are currently errors.The text was updated successfully, but these errors were encountered: