[Concurrency] AsyncSequence

The proposed design treats await as a pattern:

for await let element in myAsyncSequence {
  doSomething(element)
}
Transformed into:
var it = myAsyncSequence.makeAsyncIterator()
while let element = await it.next() {
  doSomething(element)
}

I wonder if async let could gain the same treatment. @ktoso, would this fit with the proposed structured concurrency design? We would get a basic non-customizable parallel for..in loop having the same behavior we would get by writing an async let for each element of the sequence:

for async let element in myAsyncSequence {
  doSomething(await element)
}
Transformed into:
var it = myAsyncSequence.makeAsyncIterator()
await Task.withGroup(resultType: type(of: it).Element.self) { group in
  await group.add { await it.next() }
  await group.add { await it.next() }
  ...

  while let element = await group.next() {
    doSomething(element)
  }
}
4 Likes