The designers of Kotlin also had an interesting point: await (synchronous call) should be the default, non-awaited (asynchronous) code should have a keyword indicating that it is async.
A final proposal for Rust await syntax
91–100 of 265 posts
Re: A final proposal for Rust await syntax
#92I figure the reason the proposed syntax looks gross is because other languages have been using a prefixed await for many years. The Rust decision seems well thought out though, enough to make me wonder if perhaps other languages have been doing it wrong. My first experience with futures was in the form of QFuture ( https://doc.qt.io/qt-5/qfuture.html ), and there you call .result() to block and wait for the result. T…
Lua[1], C++/Boost[2] also use "yield" it's not that uncommon and there is nothing new.
[1] https://www.lua.org/pil/9.1.html
[2] https://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/ov...
Re: A final proposal for Rust await syntax
#93Serious suggestion, and I assume you have -- have you considered ".await?" as an alternative? It won't conflict with field names and cribs on the fact that "?" changes control flow.
> The primary argument in favor of postfix was its better composability with methods and the ? operator.
Re: A final proposal for Rust await syntax
#94Re: A final proposal for Rust await syntax
#95It was glossed over why `await expr` isn't in the running; I take it it's because the language team doesn't like the necessity of adding parentheses any time you want to do postfix expressions on the await results e.g. `(await foo()).bar()`?
Re: A final proposal for Rust await syntax
#96The designers of Kotlin also had an interesting point: await (synchronous call) should be the default, non-awaited (asynchronous) code should have a keyword indicating that it is async.
The problem with this approach is merely adding the `async` keyword to a function would then significantly alter the way code is interpreted inside of the function. It seems like a non-starter to me to have adding the `async` keyword cause a function to stop compiling.
Re: A final proposal for Rust await syntax
#97Earlier quoted context omitted.
The problem with this approach is merely adding the `async` keyword to a function would then significantly alter the way code is interpreted inside of the function. It seems like a non-starter to me to have adding the `async` keyword cause a function to stop compiling.
In Kotlin this is not an issue, because you simply can't call an async function at all until you've added the async keyword (actually `suspend` in Kotlin). If you want to call an async function from a sync context, you just wrap it in an async block.
In any case, I'm a big fan of the idea that async/await is just syntax around something that can be done without the syntax (i.e. wrapping Futures that can otherwise be manipulated without the syntax).
Re: A final proposal for Rust await syntax
#98Earlier quoted context omitted.
Disambiguation is easy when `await` is a keyword. [0] ;) error[E0721]: `await` is a keyword in the 2018 edition --> src/lib.rs:2:5 | 2 | await: i32 | ^^^^^ help: you can use a raw identifier to stay compatible: `r#await` [0] https://play.rust-lang.org/?version=stable&mode=debug&editio...
Programming languages didn't used to have reserved words: https://en.wikipedia.org/wiki/Reserved_word > Not all languages have the same numbers of reserved words. For example, Java (and other C derivatives) has a rather sparse complement of reserved words—approximately 50 – whereas COBOL has approximately 400. At the other end of the spectrum, pure Prolog and PL/I have none at all. I don't really know why modern prog…
Preventing potential footguns prevents a large number of bugs. In fact, preventing a specific kind of footgun is one of the primary reasons that Rust exists.
Fewer footguns means easier to work with code. Easy to work with code means fewer bugs. If having reserved words removes more footguns than it creates than they are a good thing to have - and IMO - they do indeed prevent confusion and footguns.
As long as these footguns can be removed without giving up much in exchange, I say remove all footguns!
Re: A final proposal for Rust await syntax
#99It was glossed over why `await expr` isn't in the running; I take it it's because the language team doesn't like the necessity of adding parentheses any time you want to do postfix expressions on the await results e.g. `(await foo()).bar()`?
That was discussed in a previous post, but yes that's why. And in Rust, the biggest postfix expression is the extremely common `?` operator, expected to be used with most futures.
Re: A final proposal for Rust await syntax
#100The designers of Kotlin also had an interesting point: await (synchronous call) should be the default, non-awaited (asynchronous) code should have a keyword indicating that it is async.
There were two main objections:
First, a Rust async function like this (using explicit lifetime annotations for exposition purposes only, normally they would be elided)...
async fn f(r: &'a i32) -> i32 { ... *r ... }
...desugars to a sync function whose return value closes over its arguments: fn f(r: &'a i32) -> impl Future + 'a { ... }
Sometimes you want the future to live longer than the arguments, so you write that desguaring yourself: fn f(r: &'a i32) -> impl Future {
let i = *r; // Deal with the reference *now* before constructing the future.
async { ... i ... }
}
Ideally, functions could switch back and forth between these two forms without changing their API, for backwards compatibility reasons. This means you can't just auto-await calls to `async fn`s like Kotlin does- it would need to be a more complex rule.Second, a lot of users want suspension points to show up in the program text the same way `?` does. This is nice for managing invariants- you know when you might be interrupted. (Personally I don't think this is a good reason; the borrow checker and async-aware synchronization primitives would solve the problem with less noise, but it is what it is.)