@mattiem but, to be serious for a second, having a convenient way of turning any sync method into an async one would be cool, which this is not (convenient).
How about this lol?
(wait) no it doesn't work simply like that
@mattiem I'm not posting to the forum thread more, but I think it's a good demonstration that maybe, just maybe, moving work to the background needs to be a bit more verbose for the sake of clarify.
"clarity at the point of use is your most important goal." (which is long forgotten)
https://www.swift.org/documentation/api-design-guidelines/#fundamentals
@a_grebenyuk yeah.
I haven't thought really deeply about this. But I'm pretty sure callee-defined correctness/safety is very good, but caller-defined background work is what's missing.
(and yes, it needs to be clear, even if the compiler can prove it to be safe)
@a_grebenyuk Right. I think a little sugar could make this quite nice.
However I found a bug that makes this harder to use, and seems to apply to closure literals too
@alexito4 @a_grebenyuk I think you two are really on to something with this tension.
Today, “await” does not mean “won’t block”. It does not even mean “will be async”!
I really believe that the language is 90% of the way there. Providing a clearer mental model for non-experts PLUS some nice way for callers to be in control of blocking work are missing pieces.
@rhysmorgan yes you do!
And there is a very significant advantage to doing it that way. Because the function signature is under your control, you can do use a `sending` return and avoid needing Sendable types.
@markmalstrom Hard to say for sure because some details there aren’t finalized yet. But I’m pretty sure yes.
I still find this incredibly verbose though…
@markmalstrom I think I want something more like the example at the bottom of this post.
https://forums.swift.org/t/pitch-inherit-isolation-by-default-for-async-functions/74862/119
I'm genuinely surpised to hear these suggestions, because my experiences have been exactly the opposite. I haven't yet encountered a situation where I've wanted some kind of stateless background type. But I have regularly found places where I have this one tiny bit of synchronous work that can be expensive and can and should be done off-actor. JSONDecoder is a fantastic, real-world example. This is the critical concept. I was just imagining what would happen if we made this change without t...
Does this do what you’re wanting? (It wouldn’t have some of the subtler benefits of child tasks, but otherwise I think just adding ‘Task’ and ‘.value’ to that example makes it work.
let result = try await Task {
// this runs on the global executor
try nonIsolatedExpensiveWork()
}.value
@mattiem @markmalstrom I love the easy readability of
let result = try await {
// this runs on the global executor
try nonIsolatedExpensiveWork()
}
Maybe even not so much the executor aspect of it, but just being able to start the async closure straight away like that.
Could the executor be specified similar to what we do with MainActor now? Something like this
await { MainActor in … }
await { GlobalActor in … }
await { CurrentActor in … }
@pasi You can get really close with an immediately-invoked closure today! But what you cannot do is work in the background.
@nicklockwood Yes!!
async let will use the global executor *if* the function is non-isolated.
@ryanashcraft I kinda fell ass-backwards into realizing it today.
https://forums.swift.org/t/pitch-inherit-isolation-by-default-for-async-functions/74862/119
I'm genuinely surpised to hear these suggestions, because my experiences have been exactly the opposite. I haven't yet encountered a situation where I've wanted some kind of stateless background type. But I have regularly found places where I have this one tiny bit of synchronous work that can be expensive and can and should be done off-actor. JSONDecoder is a fantastic, real-world example. This is the critical concept. I was just imagining what would happen if we made this change without t...
@layoutSubviews Heh.
But I have learned more since then! Task.detached has a lot of side-effects you almost never want. I need a new tool!