Capturing ‘async let’ variables is not supported

Published by malhal on

Discovered this strange error in Swift, when using the sample code from the proposal which is supposed to have been accepted and implemented

  • Status: Implemented (Swift 5.5)

async let and closures

Because async let tasks cannot out-live the scope in which they are defined, passing them to closures needs some further discussion for what is legal and not.

It is legal to capture a async let in a non-escaping asynchronous closure, like this:

func greet(_ f: () async -> String) async -> String { 
   await f() 

async let name = "Alice"
await greet { await name }