-
Notifications
You must be signed in to change notification settings - Fork 14
Early return #30
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
Should return inside a
|
Absolutely not 1; either 2 or 3 - and 2 is more useful. |
2 makes most sense for me, and follows with the idea that it's a block. |
I only included 2 for completeness. I think it's immoral and is probably the hardest to implement. |
"immoral" is pretty vague and melodramatic, can you explain a bit more? Option 1 wouldn't make any sense because |
Option 2 is common in Rust, so I think it would find lots of support and not be too surprising for people. |
This was discussed in the July 2018 meeting, with both Option 2 and Option 3 being acceptable. |
I'm very sympathetic to option 2, but I worry about a couple of things:
Also, does this proposal provide a way to break out of the block body with a value? let value = do {
if (something) {
// I want to break out here with the value `42`
}
// lots of code
};
assert.equal(value, 42); Or am I forced to nest? let value = do {
if (something) {
42;
} else {
// lots of code
}
}; cc @dherman |
I’d assume you’d be forced to nest, and I’d assume it’d be forbidden in an async block too (im skeptical about async blocks at all, due to the likely need for them to behave as functions inside promises) |
Yes,
It's a corner case for sure. It depends on whether the do expression is evaluated in the context within the function, or outside the function. I think there's been discussion on this.
A
There's been discussion around the behavior of
Only if you don't want to return from the whole function, which is often what you want anyways. Otherwise, the |
Sure, but AIIFE's are a pain. |
I wasn't saying it should be an async IIFE. I was saying it should be refactored to use normal async functions instead. |
@zenparsing the outer, actual function can be async, which means that you can have an Do you mean |
@loganfsmyth Rust's |
@qm3ster I think rust's every-statement-is-an-expression paradigm was a primary inspiration for this. That's my favorite part of rust. |
But that power comes at a hefty price - semicolons. |
Each do block should be its own return as it's originally spec'd and it should not interact with the surrounding scope except to inherit ( also I saw something about it being lazy and I almost cried ). |
One of the use cases I've been thinking about is combining do expressions with pattern matching to mimic Rust's error handling. This use case would require option 2. // data: { ok: number } | { err: string }
function foo(data) {
// Unwrap the result `ok` or return the err
const result = case (data) {
when { ok } -> ok
when _ -> do {
// assuming option 2
return data
}
}
// Safely perform calculations on the wrapped ok property
return result * 5
} |
When reading code it will be required to know if the line I am scanning is within a With the main example for early return being a large do block makes me think that it is a good thing that code authors will be encouraged to break do blocks down into smaller chunks. i.e. the limitations of what you can do within a do-block likely helps prevent them from growing too large. Making them easier to read and reason about. |
Another intresting idea is that the b = true;
const a = do {
return null;
if (b) return b;
} Now the if at the end is allowed without the else because the value that the do will resolve as is already set. So this just resolves to 3 const a = do {
return 1
return 2
return 3
var b = 4; // this is fine now
} Setting a return value early seems to me like a good solution to the limitations mentioned in the proposal like declaring variables at the last line. |
Consider the following:
or even
The text was updated successfully, but these errors were encountered: