-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Proposal: rename %return
to tryreturn
#340
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
Comments
I like it. Below is other ideas that I don't like:
We could move it to a suffix operator: foo() %% |err| return err;
foo() returnerr;
foo() orreturn; But I don't like any of those ideas. I like the |
How about
D has:
If we don't have a plan to change |
I actually like the %return and %defer syntax, so I wouldn't mind keeping them. But if you really wanna get rid of sigils my only qualm is that tryreturn is not the ideal name for it. As @thejoshwolfe mentioned the 'longhand' for %return, Another option is simply a try with no parameters:
As for the defer, the scope() construct is actually quite a neat idea, though we could still call it defer
|
Try with no args looks nice, but we have a grammar problem to solve with that:
Does this
I was considering removing
I'm not sure we can pass a scope as an argument to defer. Take for example this code: fn foo(slice: []const u8) {
var y: u8 = 0;
for (slice) |x| {
defer(foo) y += x;
}
} This is equivalent to creating a closure, a big topic and something that requires heap allocation. First of all it captures the value of I believe this does work in Go, and that is because it does automatic heap allocation with garbage collection. |
Good point with passing the scope. If you're ok with the return-on-error syntax for try, the same could be done with defer:
|
I don't think bare |
One more consideration along these lines: instead of looking for the absence of a ( after the try, how about the presence of a { right after, such that:
ie, you have to do the try against the return expression of the block. The same would apply to the defer.
|
(The syntactic similarity to labels makes a little more sense with the labeled blocks proposal in #346, because control jumps in and out of the labeled thing. ... or maybe defer still a pretty special idea and #346 doesn't affect this much.) Unfortunately, this doesn't help the The "bare try" idea can work if you change the idea from |
Thanks for the discussion, all. Feel free to bring up any more points, but as it stands I think status quo is better than the proposals. |
is.readNoEof(([]u8)(result[0...])) %% @return; It passes any error to the (currently non-existing) compiler built-in |
Reasoning: people hate sigils
So the control flow with errors is then:
if statement style
provide a default
assert that it will never be an error
give the payload value or return the error
I'm happy with the if statement style and
tryreturn
in the sense that they're useful and successfully avoid sigils. Both of the%%
cases mirror??
for nullables, so it would require some thought to figure out how to not have sigils there.We also kinda want unwrap asserting to be easy because you have to do it on every printf:
Also in situations where you do want to crash easily, such as writing an application or script sort of thing, you'll use
%%
all over the place. Making error unwrapping easy is a necessary component of making the lazy, easy, default way to code, the one that handles errors correctly.Point here being, I'm not sure how we could avoid sigils and keep error unwrapping this easy.
The text was updated successfully, but these errors were encountered: