Monday, June 14, 2021

Swift Overloads That Differ Only in Async

Gwendal Roué:

I wish users could use the same method names in both synchronous and asynchronous contexts.

[…]

But I face “Invalid redeclaration” compiler errors, as expected according to the proposal.

[…]

Should I rename my async variants with some funny name? await asyncRead()? But the proposal itself wants to avoid C#’s pervasive Async suffix.

[…]

[The] new Core Data apis described in the WWDC21 conference Bring Core Data concurrency to Swift and SwiftUI face the same problem. They worked around the overload error by defining async methods with a different signature[…]

He found a workaround using the non-public @_disfavoredOverload attribute.

Ben Trumbull:

Methods in the same module cannot overload only on async-ness, but methods bridged from ObjC or in a different Swift module may.

Previously:

Comments RSS · Twitter

Leave a Comment