[swift-evolution] await keyword "scope"

Chris Lattner clattner at nondot.org
Tue Aug 29 01:19:09 CDT 2017

> On Aug 28, 2017, at 1:09 PM, Adam Kemp via swift-evolution <swift-evolution at swift.org> wrote:
> I decided to split this out to its own thread because it seems orthogonal to other issues being discussed.
> When I read this line from the proposal:
> await decodeImage(dataResource.get(), imageResource.get())
> It’s not clear to me where the asynchronous call is. There are three function calls on that line. Which ones might actually suspend? You can’t tell by looking at it because there’s only one await keyword (the answer is all 3).

Yes, this is a reasonable concern.  We debated it heavily in the Swift 2 timeframe when introducing error handling (as other’s have pointed out, it works the same way).

This is a tradeoff between the readability benefits of marking non-obvious control flow vs the readability disadvantage of having noise in the code. Requiring marking on every async or throwing call is particularly bad in the case of chaining.  Compare:

   let x = try (try (try a.foo()).bar()).baz()
   let x = try a.foo().bar().baz()

In the Swift 2 timeframe, we decided that in many cases, it is mostly obvious what APIs can throw, so one marker is enough.  That said, there ARE potentially confusing cases, and some programmers may want to be more explicit about marking.  This is why the compiler allows you to explicitly mark subexpressions if you’d like.

I believe that this design has served the community well, and I haven’t heard of serious problems with it.  I’m pretty confident that async following the same model will have similar success.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170828/fecb6a29/attachment.html>

More information about the swift-evolution mailing list