Wednesday, January 20, 2021

Swift AsyncSequence

SE-0298 (via David Smith, forum):

Swift’s async/await feature provides an intuitive, built-in way to write and use functions that return a single value at some future point in time. We propose building on top of this feature to create an intuitive, built-in way to write and use functions that return many values over time.

This proposal is composed of the following pieces:

  1. A standard library definition of a protocol that represents an asynchronous sequence of values
  2. Compiler support to use syntax on an asynchronous sequence of values
  3. A standard library implementation of commonly needed functions that operate on an asynchronous sequence of values


Update (2021-02-19): Philippe Hausler:

To facilitate the collecting of values and moving from the asynchronous world into the synchronous world we should add an extension on AsyncSequence of collect to gather up all the values asynchronously and produce an array of those values. This of course means that the function must follow the effects entailed with said asynchronous iteration; that means that when an AsyncSequence that throws is collected it will throw and when it does not throw of course that collecting process should not throw. This means that the act should be rethrows according to the conformance of the type it is called upon. Furthermore collecting all values must also be in it of itself asynchronous.

2 Comments RSS · Twitter

My fear is that Swift is becoming over-burdened by complex, edge-case language features. I loved the original (well Swift 3/4 ish) design and ergonomics of the language, but more recently they have added more and more complexity e.g. property wrappers, which probably serve a small minority of language users at the cost of making the language much harder to understand for the majority.

AsyncSequence seems like another edge case, with a high cognitive cost for the developer. Perhaps it's all worth it, but I'm not convinced.

Yes, swift comes more and more complex.
Constantly add features.
Async/await is a must to add.
But I hope they stop adding the other things

Leave a Comment