Async support in defer blocks?

Gonna shamelessly plug my pitch for async deinit, which could achieve the same, at the cost of a wrapper class (Or better yet, a ~Copyable struct).

IMO, structured concurrency is generally better presented using destructors than scopes. Like, suppose if instead of async let, you simply had a StructuredTask which would inherit cancellation when awaited, and cancel and await in its destructor. In addition to doing everything async let does, it would also:

  • Allow returning it from a function, so the caller can await it instead. Currently, this requires a different pattern where you return a Task or closure and the caller async lets it.
  • Allow having it as a member of another class. It cancels and waits when the class deinits
  • Allow adding it to an array. This has the same effect as adding a task to a TaskGroup, but without the with-style scoping. When the Array goes out of scope, the task is cancelled and awaited. An extra parameter to make it not auto-cancel could farther make the same behavior as a TaskGroup (wait all instead of cancel by default). Or just wrapping it in another class that would do it in its deinit

But yeah, await inside deinit is something you kind of expect to Just Work™.