I have a family of questions related to the new async/await and structured concurrency features in Swift.
If we make an async let declaration, the Xcode reports the type of the declaration as the type that the object will have after it's been awaited for. I.E.: If we
async let image = downloadImage()
then if we inspect image
, its type will be UIImage.
Question #1: Is there a way to pass an async let
declaration to a function as a parameter?
I would like to have a function that accepts an async let
declaration and transforms it somehow. The obvious way that comes to mind is to wrap the declaration into an async closure that includes an await of this declaration inside, but I wonder if there is a way to reference a type like Async<UIImage>
, Task<UIImage>
or something else so that an instance of this type could be declared as an async let
Question #2: Why does the Xcode report the type of an async let
as the type of value after an await instead of some metaphysical type representing the presence of "awaitable" context? Why isn't the type of async let
displayed as Task<UIImage>
? Why didn't the language go the ECMAScript path where an async function is supposed to return a Promise that can be both subscribed to and awaited?