-
-
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 and for/else implementation #23260
Closed
Closed
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
976ff84
Parse + emit code for for/else and break-with-value
tkluck d8b2553
Implement findnext() in terms of for/else and break-with-value
tkluck 8a8d4cc
Fix break-with-value in case the value expression is a symbol
tkluck 3abd4d9
Simplify break-with-value implementation: don't distinguish break-blo…
tkluck ed6ff28
Document for/else and break-with-value
tkluck 24c64f4
Fix error message from incomplete for loop
tkluck f15be30
Avoid allocating a value variable when break-block is in tail position
tkluck a67bc9f
Remove unnecessary if relating to break-labels contents
tkluck e0697d1
break-with-value: add elsebody to a few recursive expression traversals
tkluck aa48222
break-with-value: also parse 'else'-block for while loops
tkluck 29d98ab
More consistent indentation in julia-parser.scm
tkluck b801f49
break-with-value: don't emit 'nothing' as the break value from the pa…
tkluck bad3c35
Revert "Implement findnext() in terms of for/else and break-with-value"
tkluck 7c5f378
Merge remote-tracking branch 'origin/master' into break-with-value
tkluck 98d5dd7
Test execution order of break-with-value inside a try/finally block
tkluck 9af9bc2
Merge branch 'master' into break-with-value
tkluck fe10430
Merge remote-tracking branch 'origin/master' into break-with-value
tkluck 742576a
Merge branch 'master' into break-with-value
tkluck e0a42c6
break-with-value doctest fixes (whitespace, trivial output)
tkluck ef077f7
Merge remote-tracking branch 'origin/master' into break-with-value
tkluck c9934e9
Merge remote-tracking branch 'origin/master' into break-with-value
tkluck e081226
Merge remote-tracking branch 'origin/master' into break-with-value
tkluck 0aa316a
Merge remote-tracking branch 'origin/master' into break-with-value
tkluck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not related to the feature itself, but I find explicit returns easier to reason about.
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.
They are, but sometimes you don't want to factor a single loop into a function just so that you can use a
return
to get a value out. This basically lets you have that without a function.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 do like the
for else
feature but isn'tv = for ...; break i; else j end
always equivalent tofor ...; v = i; break; else v = j end
(with the correct scope ofv
)?The latter seems much easier to understand than the former. It is similar to the return value of
if else
but in that case at least the return value is easier to find.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 agree with @KristofferC that the
findnext
example isn't the best use-case for this feature because areturn
suffices; I just included it for the sake of show-casing the working patch.@yuyichao , yes that is indeed equivalent, and it's probably a matter of taste which is easier to understand.
IMHO, in a language where there is no distinction between statements and expressions, it is a bit wasteful and arguably unexpected (e.g. when compared to your
if
/else
example) not to be able to specify a value for a loop expression. That's why I personally like this feature.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.
Afterthought: it's also a bit more 'functional' because it's easier to reason about whether the resulting variable
v
is mutable or not.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.
That's why I do like the
for else
and my example above includes theelse
block.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.
Oops, I misread that. Got it.
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 do agree, however, that these cases are clearer with
return
; but they do serve as an in-situ test of the new syntax, which is useful for a WIP pull request.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.
A nice example is
intersect
, which can be improved into: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 example @rfourquet ! It could also be
which doesn't use break-with-value, but does use the
else
feature.I personally like options with
all
or with comprehensions best, but that's a matter of taste and may not give the same performance (yet).