Live data from Hacker News

Update on await syntax in Rust

boats.gitlab.io

81–90 of 199 posts

Re: Update on await syntax in Rust

#81
Excited for the feature.

Disappointed we didn't get something like a magic method (e.g., lang item) (which isn't without precedence [1] [2]) so we could have had `Future::await(fut)` or `fut.await()`.

[1] - https://manishearth.github.io/blog/2017/01/10/rust-tidbits-b... [2] - https://manishearth.github.io/blog/2017/01/11/rust-tidbits-w...

Re: Update on await syntax in Rust

#82
post #71
post #18

Earlier quoted context omitted.

My understanding of the issue (which is shallow at best) is that it's a battle between a prefixing keyword or sigil which has the problem of causing the reader to scan back and forth in the statement when chaining multiple items, which sucks), or a postfixing keyword or sigil, with dot notation for a keyword to help parsing (and everyone seems to dislike the overloading of the dot notation for this). Ignoring sigils,…

For example, on C#, I sometimes have to do: var value = (await (await startRequest()).GetBody()).Root; While the Rust syntax would make this a little more clear, I do not think it is a dealbreaker.

For ease of comparison, here's how that would look with syntax akin to Rust's choice:

    var value = startRequest().await.getBody().await.Root;

Re: Update on await syntax in Rust

#83
post #80

Earlier quoted context omitted.

Speaking as a member of the Rust language team: we specifically evaluated all syntax proposals on the assumption that many people may end up reading them without syntax highlighting. People read code in many places outside of a programmer's text editor or a code-highlighting web page, including logs, diffs, and emails.

Hopefully you didn't make a decision based on some exception use case.

The parent commenter does not suggest that this syntax was chosen based on assuming users don't have syntax highlighting, only that they did take into consideration that not all users have syntax highlighting.

Re: Update on await syntax in Rust

#84

I'm nit going to stop using rust, but I find this very disappointing. One thing I strongly disagree with - - this article said other notations would require Rust users having to learn more notation. This still requires learning more notation, just it's easy to not even be aware it exists and think a struct had a member called await instead...

Why couldn't they use a macro or a function?

EDIT: nvm. it says in TFA not the linked writeup

Re: Update on await syntax in Rust

#85
post #74
post #68

I'm not familiar with Rust, someone can explains why they went with .await? Is await a field / method on a class / struct?

None of the above. It's compiler magic. It's a keyword that says 'await on this awaitable object' (currently only a Future can be await-ed). You can read the OP which contains links to more blog posts on the reasoning. I think this is a bad decision precisely because of the questions you're asking. At least making it a 'magic method' (i.e., Future::await(...) or future.await()) with no implementation would have been…

I've been planning to try out Rust for a side project, and this decision is so principle-of-most-surprise that it's got me reconsidering, wondering what other unpleasant weirdness lurks in the language. Looking down their other options in one of the linked articles, they seem to reject a couple fine ones outright and then went with one I'd have rejected outright as being too hostile to anyone who's not quite familiar with the manual, having memorized exceptions to ordinary behavior like this. I'm struggling to understand why anyone liked this one at all, let alone enough to push it to the top of the pile.

Re: Update on await syntax in Rust

#86
post #71

Earlier quoted context omitted.

For example, on C#, I sometimes have to do: var value = (await (await startRequest()).GetBody()).Root; While the Rust syntax would make this a little more clear, I do not think it is a dealbreaker.

this makes my eyes bleed

Why?

Re: Update on await syntax in Rust

#87

I'm nit going to stop using rust, but I find this very disappointing. One thing I strongly disagree with - - this article said other notations would require Rust users having to learn more notation. This still requires learning more notation, just it's easy to not even be aware it exists and think a struct had a member called await instead...

It's not just learning the new notation. It's also that you probably can't override it with your own implementation (useful perhaps in embedded environments).

Re: Update on await syntax in Rust

#88
A bit off-topic: Is there any theoretical reason you need async / await syntax at all?

(It's certainly desirable for performance and compatibility to avoid making all subroutines into coroutines, so I understand why most languages have done this.)

And restricting it to the case where coroutines have a single return...

Subroutines are naturally coroutines that don't yield. And it seems like the question of whether it's a subroutine or a coroutine shouldn't be something the programmer needs to worry about.

What I've seen in coroutine libraries is the main reason we need await seems to be for cases when we don't want to await a result.

If you need an unawaited invocation to pass to a routine like gather, you do this:

    my_results = await gather(run_job(x) for x in work)
But, even if we can't infer from the type of gather that it must accept a future, a function can have a method that asks it to return a future:

    my_results = gather(run_job.future(x) for x in work)
You'd often want to dispense with gather. If I have these statements:

    alpha = run_alpha()
    beta = run_beta()
    gamma = run_gamma()
    return alpha + beta + gamma
In most cases, e.g. if these were requests going over the network, we'd prefer to schedule all three at once. If we want to sequence side-effects, then explicit await makes sense:

    alpha = run_alpha.await()
    beta = run_beta.await()
    gamma = run_gamma.await()
    return alpha + beta + gamma
That makes the most concurrent option easiest, as opposed to the clunky idiom of "gathering" many results.

Re: Update on await syntax in Rust

#89

I'm nit going to stop using rust, but I find this very disappointing. One thing I strongly disagree with - - this article said other notations would require Rust users having to learn more notation. This still requires learning more notation, just it's easy to not even be aware it exists and think a struct had a member called await instead...

I come from the Node world (I'm not really familiar with Rust at all), where the syntax is "await foo()", and to be honest I really like the .await syntax. First, as others have stated it makes the operator precedence clearer in my opinion. But more importantly, in my brain it makes it feel like await is a "member operator" of Promises (or I guess futures in the Rust case), so I can think "Function returns a promise, and then "calling" await on it yields and then returns the resolved object when done".

Re: Update on await syntax in Rust

#90
post #71
post #18

Earlier quoted context omitted.

My understanding of the issue (which is shallow at best) is that it's a battle between a prefixing keyword or sigil which has the problem of causing the reader to scan back and forth in the statement when chaining multiple items, which sucks), or a postfixing keyword or sigil, with dot notation for a keyword to help parsing (and everyone seems to dislike the overloading of the dot notation for this). Ignoring sigils,…

For example, on C#, I sometimes have to do: var value = (await (await startRequest()).GetBody()).Root; While the Rust syntax would make this a little more clear, I do not think it is a dealbreaker.

Please don't do that. I had to look several times to rewrite it as

    var temp1 = await StartRequest();
    var temp2 = await temp1.GetBody();
    var value = temp2.Root;
which is instantly grokked.
Post reply on HN