ph1ps
(Philipp Gabriel)
June 23, 2025, 5:00am
1
I recently stumbled upon this macro which I did not notice in previous Swift versions: Task(name:priority:) | Apple Developer Documentation .
Since the documentation is quite scarce, I tried looking for the proposal that introduced this, but I couldn‘t find one. Would someone mind explaining what this does, or point me the a source with more explanation to how it works?
Thank you.
Edit: It seems to be an experimental feature, so I guess that is the reason there is not much material out, yet.
1 Like
Pippin
(Ethan Pippin)
June 23, 2025, 6:04am
2
Below is the implementation PR that provides more motivation.
main
← hborla:concurrency-sugar
opened 03:21AM - 02 Mar 25 UTC
It's very common to wrap an entire function body in a new task, e.g. in a synchr… onous event handler that needs to kick off async work. This change adds an experimental function body macro that wraps an entire function body in a task.
This macro is a much simpler version of the `@asyncHandler` attribute, which was proposed [in an early pitch of SE-0297: Concurrency Interoperability with Objective-C](https://forums.swift.org/t/concurrency-interoperability-with-objective-c/41616#inference-of-asynchandler-on-objective-c-methods-8), and subsetted out because many event handlers need to start work synchronously. This macro currently uses `Task.init`, but could be updated to use the [pitched `Task.startSynchronously` API](https://forums.swift.org/t/pitch-concurrency-starting-tasks-synchronously-from-caller-context/77960).
This macro is gated behind the `ConcurrencySyntaxSugar` experimental feature. I'll write up a pitch for this soon to discuss the design. Please save any design feedback for the forthcoming forum thread.
I'm currently working on lifting the limitation that function body macros cannot be applied to closures, so this macro can be used with closures too. There are quite a few places that assume an attached macro can only be applied to a declaration :)
4 Likes
ktoso
(Konrad 'ktoso' Malawski 🐟🏴☠️)
June 23, 2025, 9:33am
3
Yeah, this has not gone through a evolution proposal so one should not assume this will stay in its current form, any removal/rename/change may still happen to this -- as indicated by the experimental status.
1 Like
ktoso
(Konrad 'ktoso' Malawski 🐟🏴☠️)
June 25, 2025, 1:53am
4
For what it's worth we're removing that experimental macro Thanks for pointing out it was showing up looking as if it was not experiemntal in documentation, that's a another thing we need to look at in the future. Remove the experimental @Task macro by ktoso · Pull Request #82437 · swiftlang/swift · GitHub
5 Likes