My concern with that approach is that it is very easy to get wrong. The default behavior is that if you start on one thread/queue you can end up on another. I think if we ship that then it will be extremely error prone.
The equivalent in C# would just be this:
async void ButtonClick() {
var data = await loadData(...);
var image = await process(data);
uiElement.image = image;
}
Note that to get this behavior neither the caller (this UI code) nor the callee has to do anything special. The default is to return to the originating "synchronization context", which may be a particular thread (like the UI thread) or a thread pool. You have to go out of your way to override this default:
async void ButtonClick() {
var data = await loadData(...).ConfigureAwait(false); // Don't capture the current context
var image = await process(data);
// Here we may be on the wrong thread so we would have to return to the right thread
BeginInvokeOnMainThread(() => {
uiElement.image = image;
});
}
Like Joe I believe it's possible to make Swift behave this way without tying us to particular API, and I think it should be a requirement in order to make this feature usable. One way to do it would be to use pattern matching in the compiler with a future-like type. There may be another way too. I haven't looked at how Kotlin handles this. My hope is that it ends up being very low friction for app developers and framework authors to adopt.