Remove Ok(()) with .await as end statement
Make app.listen more ergonomic.
Before
#[async_std::main]
async fn main() -> Result<(), std::io::Error> {
let mut app = tide::new();
app.at("/").get(|_| async { Ok("Hello, world!") });
app.listen("127.0.0.1:8080").await?;
Ok(())
}
After
#[async_std::main]
async fn main() -> Result<(), std::io::Error> {
let mut app = tide::new();
app.at("/").get(|_| async { Ok("Hello, world!") });
app.listen("127.0.0.1:8080").await
}
I'm not sure if we should do this -- ? marks where errors come from, and that's worthwhile having even if the result is smaller.
The lang team is exploring options to remove the need for Ok(()) at the end of functions, which I think is what we really want to solve here. Until then I think keeping this as-is may be good enough (and can also serve as an example of where Ok-wrapping could really help).
The output is Result<(), std::io::Error> which is exactly the same type as app.listen("127.0.0.1:8080").await. I feel like it is easier to read this way since it will exit when there is Err returning from listen.
I don't think it is necessary to use ? here, ? is useful but here it is similar like using return statement; as the last statement, so it may just be better to return the result directly. I personally think ? is useful when propagating errors halfway, note that quite some functions also returns Result directly such as Result::and_then.
Regarding the lang team solving Ok(()) at the end of functions does not matter, I feel like removing ?; and Ok(()) makes it more explicit.
+1 to the suggestion.
app.listen("127.0.0.1:8080").await?;
Ok(())
makes it unclear what app.listen returns, without looking it up in the docs. do I need to consume something there? Is it something important?
app.listen("127.0.0.1:8080").await
in contrast makes it clear with the context given that it returns an io::Result<()> so there is no data to look for coming form app.listen.
Yes, ? does a conversion for the error with From type that may not be implicit, but app.listen("127.0.0.1:8080").await makes it explicit that it returns io::Error.
Edit: The other reason being the cool factor, how cool is it to be able to listen as the last return and it looks quite idiomatic and mind-blown to me.
We should probably look to having a return value of Result<!, std::io::Error> ~~for either tide 1.0 or 2.0,~~ depending on when never stabilizes, as it is the best answer to this issue.
Edit: I realized this morning that would be more on async_std's end.