Skip to content

Resolve await syntax #57640

Closed
Closed

Description

@cramertj

Before commenting in this thread, please check #50547 and try to check that you're not duplicating arguments that have already been made there.


Notes from shepherds:

If you're new to this thread, consider starting from #57640 (comment), which was followed by three great summary comments, the latest of which were #57640 (comment). (Thanks, @traviscross!)

Activity

  1. added
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    T-langRelevant to the language team
    on Jan 15, 2019
  2. mehcode commented on Jan 15, 2019

    @mehcode
    Contributor

    I thought it might be useful to write up how other languages handle an await construct.


    Kotlin

    val result = task.await()

    C#

    var result = await task;

    F#

    let! result = task()

    Scala

    val result = Await.result(task, timeout)

    Python

    result = await task

    JavaScript

    let result = await task;

    C++ (Coroutines TR)

    auto result = co_await task;

    Hack

    $result = await task;

    Dart

    var result = await task;
    

    With all that, let's remember that Rust expressions can result in several chained methods. Most languages tend to not do that.

  3. lnicola commented on Jan 15, 2019

    @lnicola
    Member

    With all that, let's remember that Rust expressions can result in several chained methods. Most languages tend to not do that.

    I'd say that languages that support extension methods tend to have them. These would include Rust, Kotlin, C# (e.g. method-syntax LINQ and various builders) and F#, although the latter heavily uses the pipe operator for the same effect.

  4. mehcode commented on Jan 15, 2019

    @mehcode
    Contributor

    Purely anecdotal on my part but I regularly run in to dozen+ method chained expressions in Rust code in the wild and it reads and runs fine. I haven't experienced this elsewhere.

  5. earthengine commented on Jan 15, 2019

    @earthengine

    I would like to see that this issue was refered in the top post of #50547 (beside the check box "Final syntax for await.").

  6. kvinwang commented on Jan 16, 2019

    @kvinwang

    Kotlin

    val result = task.await()

    Kotlin's syntax is:

    val result = doTask()

    The await is just a suspendable function, not a first-class thing.

  7. mehcode commented on Jan 16, 2019

    @mehcode
    Contributor

    Thank you for mentioning that. Kotlin feels more implicit because futures are eager by default. It's still however a common pattern in a deferred block to use that method to wait on other deferred blocks. I've certainly done it several times.

  8. scottmcm commented on Jan 16, 2019

    @scottmcm
    Member

    @cramertj Since there are 276 comments in #50547, could you summarize the arguments made there to make it easier to not repeat them here? (Maybe add them to the OP here?)

  9. chpio commented on Jan 16, 2019

    @chpio
    Contributor

    Kotlin feels more implicit because futures are eager by default. It's still however a common pattern in a deferred block to use that method to wait on other deferred blocks. I've certainly done it several times.

    maybe you should add both use cases with a bit of context/description.

    Also what's with other langs using implicit awaits, like go-lang?

  10. 197g commented on Jan 16, 2019

    @197g
    Contributor

    One reason to be in favour of a post-fix syntax is that from the callers perspective, an await behaves a lot like a function call: You relinquish flow control and when you get it back a result is waiting on your stack. In any case, I'd prefer a syntax that embraces the function-like behaviour by containing function-paranthesis. And there are good reasons to want to split construction of coroutines from their first execution so that this behaviour is consistent between sync and async blocks.

    But while the implicit coroutine style has been debated, and I'm on the side of explicitness, could calling a coroutine not be explicit enough? This probably works best when the coroutine is not directly used where constructed (or could work with streams). In essence, in contrast to a normal call we expect a coroutine to take longer than necessary in a more relaxed evaluation order. And .await!() is more or less an attempt to differentiate betwen normal calls and coroutine calls.

    So, after hopefully having provided a somewhat new take on why post-fix could be preferred, a humble proposal for syntax:

    • future(?)
    • or future(await) which comes with its own tradeoffs of course but seems to be accepted as less confusing, see bottom of post.

    Adapting a fairly popular example from other thread (assuming the logger.log to also be a coroutine, to show what immediately calling looks like):

    async fn log_service(&self) -> T {
       let service = self.myService.foo(); // Only construction
       self.logger.log("beginning service call")(?);
       let output = service(?); // Actually wait for its result
       self.logger.log("foo executed with result {}.", output)(?);
       output
    }
    

    And with the alternative:

    async fn log_service(&self) -> T {
       let service = self.myService.foo(); // Only construction
       self.logger.log("beginning service call")(await);
       let output = service(await);
       self.logger.log("foo executed with result {}.", output)(await);
       output
    }
    

    To avoid unreadable code and to help parsing, only allow spaces after the question mark, not between it and the open paran. So future(? ) is good while future( ?) would not be. This issues does not arise in the case of future(await) where all current token can be used as previously.

    The interaction with other post-fix operators (such as the current ?-try) is also just like in function calls:

    async fn try_log(message: String) -> Result<usize, Error> {
        let logger = acquire_lock()(?);
        // Very terse, construct the future, wait on it, branch on its result.
        let length = logger.log_into(message)(?)?;
        logger.timestamp()(?);
        Ok(length)
    }
    

    Or

    async fn try_log(message: String) -> Result<usize, Error> {
        let logger = acquire_lock()(await);
        // Very terse, construct the future, wait on it, branch on its result.
        let length = logger.log_into(message)(await)?;
        logger.timestamp()(await);
        Ok(length)
    }
    

    A few reasons to like this:

    • Compared to .await!() it does not allude to a member that could have other uses.
    • It follows natural precedence of calls, such as chaining and use of ?. This keeps the number of precendence classes lower and helps with learning. And function calls have always been somewhat special in the language (even though the have a trait), so that there is no expectation of user code being able to define their own my_await!() that has very similar syntax and effect.
    • This could generalize to generators and streams, as well as generators that expect more arguments to be provided on resumption. In essence, this behaves as an FnOnce while Streams would behave like a FnMut. Additional arguments may also be accomodated easily.
    • For those who have used current Futures before, this captures how a ? with Poll should have worked all along (unfortunate stabilization sequence here). As a learning step, it is also consistent with expecting a ? based operator to divert control flow. (await) on the other hand would not satisfy this but afterall the function will always expect to resume at the divergent point.
    • It does use a function-like syntax, though this argument is only good if you agree with me 😄

    And reasons not to like this:

    • ? appears to be an argument but it is not even applied to an expression. I believe this could be solved through teaching, as the token it appears to be applied to is the function call itself, which is the somewhat correct notion. This also positively means that the syntax is unambiguous, I hope.
    • More (and different) mix of paranthesis and ? can difficult to parse. Especially when you have one future returning a result of another future: construct_future()(?)?(?)?. But you could make the same argument for being able to a result of an fn object, leading to expression such as this being allowed: foobar()?()?()?. Since nevertheless I've never seen this used nor complaint, splitting into separate statements in such cases seems to be required rarely enough. This issues also does not exist for construct_future()(await)?(await)?-
    • future(?) is my best shot at a a terse and still somewhat concise syntax. Yet, its reasoning is grounded on implementation details in coroutines (temporarily returning and dispatching on resume), which might make it unsuitable for an abstraction. future(await) would be an alternative that could still be explainable after await has been internalized as a keyword but the argument position is a bit hard to swallow for me. It could be fine, and it is certainly more readable when the coroutine returns a result.
    • Interference with other function call proposals?
    • Your own? You need not like it, it just felt like a waste to not at least propose this terse post-fix syntax.
  11. Pauan commented on Jan 17, 2019

    @Pauan

    future(?)

    There's nothing special about Result: Futures can return any Rust type. It just so happens that some Futures return Result

    So how would that work for Futures which don't return Result?

  12. 197g commented on Jan 17, 2019

    @197g
    Contributor

    It seems it was not clear what I meant. future(?) is what was previously discussed as future.await!() or similar. Branching on a future that returns a result as well would be future(?)? (two different ways how we can relinquish control flow early). This makes future-polling (?) and result testing? orthogonal. Edit: added an extra example for this.

  13. Pauan commented on Jan 17, 2019

    @Pauan

    Branching on a future that returns a result as well would be future(?)?

    Thanks for clarifying. In that case I'm definitely not a fan of it.

    That means that calling a function which returns a Future<Output = Result<_, _>> would be written like foo()(?)?

    It's very syntax-heavy, and it uses ? for two completely different purposes.

  14. 490 remaining items

  15. ben0x539 commented on Feb 14, 2019

    @ben0x539
    Contributor

    We could introduce a trigraph like ... for users on keyboard layouts where ~ is inconvenient.

  16. norcalli commented on Feb 14, 2019

    @norcalli

    At first, I was strongly on the side of having a delimiter-required prefix syntax as await(future) or await{future} since it is so unambiguous and easy to visually parse. However, I understand the proposals by others that a Rust Future is not like most other Futures from other languages, since it does not put a task onto an executor immediately, but instead is more of a control flow structure which transforms the context into what is homomorphic to a monad call chain essentially.

    It makes me think that it is somewhat unfortunate that now there is a confusion in attempting to compare it to other languages with that regard. The closest analog really is the monad do notation in Haskell or the for comprehension in Scala (which are the only ones I'm familiar with off the top of my head). Suddenly, I appreciate the consideration of proposing a unique syntax, but I'm afraid that the existence of the ? operator has both encouraged and discouraged the use of other sigils with it. Any other sigil based operators next to ? make it look noisy and confusing, such as future@?, but the precedent set by having a postfix sigil operator means that another one is not so ridiculous.

    I have, therefore, been convinced of the merit of the postfix sigil operator. The downside of this, is that the sigil I would've most preferred is taken by the never type. I would've preferred ! since I think that future!? would make me chuckle whenever I wrote it, and it makes the most visual sense to me to see. I suppose $ would be next, then since it is visually discernable future$?. Seeing ~ still reminds me of the early days of Rust when ~ was the prefix operator for heap allocation. It's all very personal, though, so I don't envy the final decision makers. Were I them, I would probably just go with the inoffensive choice of the prefix operator with required delimiters.

  17. lnicola commented on Feb 14, 2019

    @lnicola
    Member

    However, I understand the proposals by others that a Rust Future is not like most other Futures from other languages, since it does not put a task onto an executor immediately, but instead is more of a control flow structure which transforms the context into what is homomorphic to a monad call chain essentially.

    I tend to disagree. The behaviour you mention is not a property of await, but of the surrounding async function or scope. It's not await that delays the execution of the code preceding it, but the scope that contains said code.

  18. I60R commented on Feb 15, 2019

    @I60R

    Probably the problem with weirdly looking @ symbol is that using it in that context was never been expected before, therefore in most fonts it's provided in so uncomfortable for us shape.

    Then providing a better glyph and some ligatures for popular programming fonts (or at least for Mozilla's Fira Code) might improve situation a bit.

    In all other cases, for me it don't looks that @ is so weird to cause any real problems when writing or maintaining code.


    E.g. the following code uses different than @ symbol - @:

    // A
    if db.is_trusted_identity(recipient.clone(), message.key.clone())@? {
        info!("recipient: {}", recipient);
    }
    
    // B
    match db.load(message.key)@? {
        Some(key) => key,
        None => {
            return Err(/* [...] */);
        }
    };
    
    // C
    let mut res = client.get(&script_src)
        .header("cookie", self.cookies.read().as_header_value())
        .header("user-agent", USER_AGENT)
        .send()@?
        .error_for_status()?;
    
    // D
    let mut res: InboxResponse = client.get(inbox_url)
        .headers(inbox_headers)
        .send()@?
        .error_for_status()?
        .json()@?;
    
    // E
    let mut res: Response = client.post(url)
        .multipart(form)
        .headers(headers.clone())
        .send()@?
        .error_for_status()?
        .json()@?;
    
    // F
    async fn request_user(self, user_id: String) -> Result<User> {
        let url = format!("users/{}/profile", user_id);
        let user = self.request(url, Method::GET, None, true)@?
            .res.json::<UserResponse>()@?
            .user
            .into();
    
        Ok(user)
    }
    Expand for comparison how it looks with regular ANSI `@`
    // A
    if db.is_trusted_identity(recipient.clone(), message.key.clone())@? {
        info!("recipient: {}", recipient);
    }
    
    // B
    match db.load(message.key)@? {
        Some(key) => key,
        None => {
            return Err(/* [...] */);
        }
    };
    
    // C
    let mut res = client.get(&script_src)
        .header("cookie", self.cookies.read().as_header_value())
        .header("user-agent", USER_AGENT)
        .send()@?
        .error_for_status()?;
    
    // D
    let mut res: InboxResponse = client.get(inbox_url)
        .headers(inbox_headers)
        .send()@?
        .error_for_status()?
        .json()@?;
    
    // E
    let mut res: Response = client.post(url)
        .multipart(form)
        .headers(headers.clone())
        .send()@?
        .error_for_status()?
        .json()@?;
    
    // F
    async fn request_user(self, user_id: String) -> Result<User> {
        let url = format!("users/{}/profile", user_id);
        let user = self.request(url, Method::GET, None, true)@?
            .res.json::<UserResponse>()@?
            .user
            .into();
    
        Ok(user)
    }
  19. Pzixel commented on Feb 15, 2019

    @Pzixel

    @norcalli

    At first, I was strongly on the side of having a delimiter-required prefix syntax as await(future) or await{future} since it is so unambiguous and easy to visually parse.

    Then you probably want unambiguous if { cond }, while { cond } and match { expr }...

    However, I understand the proposals by others that a Rust Future is not like most other Futures from other languages

    It's not true. It's actually is like most other futures from other languages. Difference between "run until first await when spawned" vs "run until first await when polled" is not that big. I know because I worked with both. If you actually think "when this difference comes to play" you will find only corner case. e.g. you will get error when creating future on first poll instead of when it's created.

    They could be internally different, but they are quite the same from user perspective so it doesn't make sense to me to make a distinction here.

  20. CAD97 commented on Feb 15, 2019

    @CAD97
    Contributor

    @Pzixel

    Difference between "run until first await when spawned" vs "run until first await when polled" is not that big.

    That's not the difference we're talking about, though. We're not talking about lazy vs eager -to-first-await.

    What we're talking about is that await joins the awaited Promise (JS) / Task (C#) on the executor in other languages, which was already put on the executor at construction (so was already running in the "background"), but in Rust, Futures are inert state machines until await!-driven.

    Promise/Task is a handle to a running asynchronous operation. Future is a deferred asynchronous computation. People, including notable names in Rust, have made this mistake before, and examples have been linked before in the middle of these 500+ comments.

    Personally, I think this mismatch of semantics is large enough to counteract the familiarity of await. Our Futures accomplish the same goal as Promise/Task, but though a different mechanism.


    Anecdotally, for me when I first learned async/await in JavaScript, async was "just" something I wrote to get the await superpower. And the way I was taught to get parallelism was a = fa(); b = fb(); /* later */ await [a, b]; (or whatever it is, it's been an age since I've had to write JS). My posit is that other people's view of async lines up with me, in that Rust's semantics don't mismatch on async (give you await superpower), but on Future construction and await!.


    At this point, I believe the discussion on the differences in Rust's async/Future/await semantics has run its course, and no new information is being presented. Unless you have a new position and/or insight to bring, it'd probably be best for the thread if we leave that discussion here. (I'd be happy to take it to Internals and/or Discord.)

  21. Pzixel commented on Feb 15, 2019

    @Pzixel

    @CAD97 yes, I see your position, but I think the discinction is not that big.

    You got me, I got you. So let the discussion have its flow.

  22. orthoxerox commented on Feb 15, 2019

    @orthoxerox

    @CAD97

    People, including notable names in Rust, have made this mistake before, and examples have been linked before in the middle of these 500+ comments.

    If even people intimately familiar with Rust make that mistake, is it really a mistake?

  23. nikomatsakis commented on Feb 15, 2019

    @nikomatsakis
    Contributor

    So, we had a number of discussions about async-await at the Rust All Hands. In the course of those discussions, a few things became clear:

    First, there is no consensus (yet) in the lang team about the await syntax. There are clearly a lot of possibilities and strong arguments in favor of all of them. We spent a long time exploring alternatives and produced quite a lot of interesting back-and-forth. An immediate next step for this discussion, I think, is to convert those notes (along with other comments from this thread) into a kind of summary comment that lays out the case for each variant, and then to continue from there. I'm working with @withoutboats and @cramertj on that.

    Stepping back from the syntax question, another thing that we plan to do is an overall triage of the status of the implementation. There are a number of current limitations (e.g., the implementation requires TLS under the hood presently to thread information about the waker). These may or may not be blockers to stabilization, but regardless they are issues that do need to ultimately be addressed, and that is going to require some concerted effort (in part from the compiler team). Another next step then is to conduct this triage and generate a report. I expect us to conduct this triage next week, and we'll have an update then.

    In the meantime, I am going to go ahead and lock this thread until we've had a chance to produce the above reports. I feel like the thread has already served its purpose of exploring the possible design space in some depth and further comments aren't going to be particularly helpful as this stage. Once we have the aforementioned reports in hand, we will also lay out the next steps towards reaching a final decision.

    (To expand a bit on the final paragraph, I am pretty interested in exploring alternative ways to explore the design space beyond long discussion threads. This is a much bigger topic than I can address in this comment, so I won't go into details, but suffice to say for now that I am quite interested in trying to find better ways to resolve this -- and future! -- syntax debates.)

  24. locked and limited conversation to collaborators on Feb 15, 2019
  25. nikomatsakis commented on Mar 1, 2019

    @nikomatsakis
    Contributor

    Async-await status report:

    http://smallcultfollowing.com/babysteps/blog/2019/03/01/async-await-status-report/

    Relative to my previous comment, this contains the results of the triage and some thoughts about syntax (but not yet a full syntax write-up).

  26. nikomatsakis commented on Mar 5, 2019

    @nikomatsakis
    Contributor

    Marking this issue as blocking for async-await stabilization, at least for the time being.

  27. withoutboats commented on Apr 29, 2019

    @withoutboats
    Contributor

    Quite a while ago, Niko promised that we would write a summary of the discussion in the language team and the community about the final syntax for the await operator. Our apologies for the long wait. A write up of the status of the discussion is linked below. Before that, though, let me also give an update on where the discussion stands now and where we will go from here.

    Brief summary of where async-await stands right now

    First, we hope to stabilize async-await in the 1.37 release , which branches on July 4th, 2019. Since we do not want to stabilize the await! macro, we have to resolve the syntax question before then. Note that this stabilization doesn’t represent the end of the road — more the beginning. There remains feature work to be done (e.g., async fn in traits) and also impl work (continued optimization, bug-fixing, and the like). Still, stabilizing async/await will be a major milestone!

    As far as the syntax goes, the plan for resolution is as follows:

    • To start, we are publishing a write-up of the syntax debate thus far -- please take a look.
    • We want to be forward compatible with obvious future extensions to the syntax: processing streams with for loops in particular (like JavaScript's for await loop). That's why I've been working on a series of posts about this issue (first post here and more coming in the future).
    • At the upcoming lang-team meeting on May 2, we plan to discuss the interaction with for loops and also to establish a plan for reaching a final decision on the syntax in time to stabilize async/await in 1.37. We'll post an update after the meeting to this internals thread.

    The writeup

    The writeup is a dropbox paper document, available here. As you'll see, it is fairly long and lays out a lot of the arguments back-and-forth. We would appreciate feedback on it; rather than re-opening this issue (which already has more than 500 comments) I've created an internals thread for that purpose.

    As I said before, we plan to reach a final decision in the near future. We also feel the discussion has largely reached a stable state: expect the next few weeks to be the "final comment period" for this syntax discussion. After the meeting we'll hopefully have a more detailed timeline to share for how this decision will be made.

    Async/await syntax is probably the most hotly anticipated feature Rust has gained since 1.0, and the syntax for await in particular has been one of the decisions on which we have received the most feedback. Thank you to everyone who has participated in these discussions over the last few months! This is a choice on which many people have strongly divergent feelings; we want to assure everyone that your feedback is being heard and the final decision will be reached after much thoughtful and careful deliberation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitAsyncAwait-PolishAsync-await issues that are part of the "polish" areaC-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-langRelevant to the language team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions