-
Notifications
You must be signed in to change notification settings - Fork 1.6k
RFC: throw
expressions
#2426
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
Closed
+923
−0
Closed
RFC: throw
expressions
#2426
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9e1fcb6
rfc, throw-expr: initial version.
Centril b2c77b5
rfc, throw-expr: simplyify tiny bit.
Centril c100f5e
rfc, throw-expr: add unresolved questions, discussion 'yield throw', …
Centril 1f8a9d4
rfc, throw-expr: fix interlinks.
Centril 0e1cb99
rfc, throw-expr: fix references to RFC 243
Centril 64aa79a
rfc, throw-expr: better breakage analysis and some initial discussion…
Centril 09a2dec
rfc, throw-expr: drawback: no early-return Ok-wrapping.
Centril da7e202
rfc, throw-expr: add unresolved question: `throw 'label expr`
Centril 1767e6e
rfc, throw-expr: also reserve `fail` and leave the choice of keyword …
Centril 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 hidden or 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.
Or, go the other way around- let people
break
out oftry
blocks. On its own, this is probably a bad idea, astry
blocks are not loops, but it's possible'label: try { .. break 'label value; .. }
would be sufficient. For that matter, maybe anyreturn
orbreak
that leaves thetry
block should beOk
-wrapped? For example:Maybe that's even worse.
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.
My main concern wrt. re-purposing
break
is this:Probably, yeah.
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.
Regarding break with label:
That seems workable, but it is not particularly ergonomic.
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.
While I'm a fan of having label-break-value, I see it mostly as a way for macros to provide customized flow control constructs. I'd rather it typically be internal to libraries more than something that shows up in syntax that would be used broadly.
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.
Another, possibly bad, idea to facilitate for macros could be to introduce the special label
'try
which always refers to the closesttry { .. }
block.That way, you could just write:
where
ok!(value)
expands tobreak 'try Try::from_ok(value)
.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.
@Centril I think my rigorous reason is I don't like introducing new names "out of thin air." Every other label has its name chosen by the source code- I would be much less bothered by some kind of
'my_label: fn f() { ... }
or'my_label: try { ... }
than I am by introducing'fn
or'try
as "special" names.A similar-but-different situation came up in #2115, where people felt wary about allowing the source code to introduce names without some primary declaration point. But in that case, the source code is still the thing determining the name- you write the same name in two places and they mean the same thing. Labels inherently have a "declaration" point, but the same reasoning applies- you write the name down in both places, as a label.
(@squishy-clouds
'static
is not a label at all- it's a lifetime, and further kind of a "monoidal identity element" like0
or1
or[]
, so it gets a bit of a pass here.)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.
@rpjohnst I don't think they would be so "out of thin air";
If the system of having special labels is applied out consistently per control flow block-form you could have:
'fn
'loop
'while
'for
loop
?'if
'match
'try
'async
'const
(maybe not?)The goal of these are not to be used directly in people's code, but rather hidden away in macros so that you can invent new and interesting DSLs. The problem with having to explicitly declare the labels is that you have to pass them into the macros.
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.
Do we have any actual use cases for a generalized "magic labels" feature like this, even in DSLs? I've never heard anyone suggest any of these except as an implementation detail of a throw/fail macro.
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.
One use-case might be that if we had a
'try
built-in label that can be targeted by?
, I think all of the other exception like syntax can be implemented by the eco-system (or evenstd
if deemed worthy) as macros. That would allow for:?
with these blocks without auto-wrapping or any other additional functionality, basically keeping it as close to current control flow as possible.std
. Since they would still be macros, there's still an opt-in to additional semantics like auto-converting a final result. Adjusting pieces ofstd
also seems easier and more future proof than changing the language itself.I'm wondering if it would make sense to put together a proposal for this path for the new exception like syntax extensions.
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 very concretely at least; Just hypothesizing ;)