# Expected Behavior of Async Overloads in Protocol Conformance Contexts?

**URL:** <https://forums.swift.org/t/expected-behavior-of-async-overloads-in-protocol-conformance-contexts/53236>\
**Category:** Using Swift\
**Tags:** concurrency\
**Created:** [November 2, 2021, 2:09pm UTC](https://forums.swift.org/t/expected-behavior-of-async-overloads-in-protocol-conformance-contexts/53236 "2021-11-02T14:09:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![atierian](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/atierian/32/20271_2.png) [@atierian](https://forums.swift.org/u/atierian)\
**Post date:** [November 2, 2021, 2:09pm UTC](https://forums.swift.org/t/expected-behavior-of-async-overloads-in-protocol-conformance-contexts/53236/1 "2021-11-02T14:09:34Z")

</div>

Async method overloads in the context of protocol conformance behave differently than the [Async/await proposal](https://github.com/apple/swift-evolution/blob/main/proposals/0296-async-await.md) and subsequent amendment [Allow overloads that differ only in async](https://forums.swift.org/t/amendment-se-0296-allow-overloads-that-differ-only-in-async/49808) suggest they do.

This overload works as expected; there are no compiler errors and calling context inference resolves as outlined in the proposal.

```swift
struct Foo { 
  func doSomething() -> String { "hello" }
  func doSomething() async -> String { "world" }
}

```

However, things fall apart when a protocol is introduced.

```swift
protocol P { // ✅ this is fine
  func doSomething() -> String
  func doSomething() async -> String
}

struct Bar: P { // ❌ Type `Bar` does not conform to protocol `P`
  func doSomething() -> String { "hello" } // ❌ candidate exactly matches
  func doSomething() async -> String { "world" } // ❌ candidate exactly matches
}

```

This seems to be due to the fact that `sync` methods can satisfy `async` method conformance requirements, as outlined in the [Protocol Conformance](https://github.com/apple/swift-evolution/blob/main/proposals/0296-async-await.md#protocol-conformance) section of the Async/await proposal.

I don't believe this is expected behavior based on the acceptance reasoning of [Allow overloads that differ only in async](https://forums.swift.org/t/accepted-amendment-to-se-0296-allow-overloads-that-differ-only-in-async/50117). Specifically, creating an exception to the async overloading rule for protocol conformance violates this idea:

> Without overloading on `async` , the Swift ecosystem will end up with a nontrivial number of APIs with non-ideal names (e.g., an `Async` suffix) that won't be able to be fixed later.

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [November 2, 2021, 2:43pm UTC](https://forums.swift.org/t/expected-behavior-of-async-overloads-in-protocol-conformance-contexts/53236/2 "2021-11-02T14:43:50Z")

</div>

This may well be [[SR-15150] Async overloading in protocol implementation fails · Issue #57476 · apple/swift · GitHub](https://bugs.swift.org/browse/SR-15150).

---

<div class="post-metadata">

**Author:** ![atierian](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/atierian/32/20271_2.png) [@atierian](https://forums.swift.org/u/atierian)\
**Post date:** [November 2, 2021, 2:53pm UTC](https://forums.swift.org/t/expected-behavior-of-async-overloads-in-protocol-conformance-contexts/53236/3 "2021-11-02T14:53:56Z")

</div>

Yes, that's the same issue. Thanks for the link!  
I really hope this gets addressed; the current state prevents transitioning to using async / await in certain circumstances.

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [November 2, 2021, 3:07pm UTC](https://forums.swift.org/t/expected-behavior-of-async-overloads-in-protocol-conformance-contexts/53236/4 "2021-11-02T15:07:17Z")

</div>

Yes. The issue was turned into an Apple "Radar" bug, so it is tracked somewhere. This does not mean it will be fixed. You can vote for the issue on [bugs.swfit.com](http://bugs.swfit.com), and report a duplicate on [http://feedbackassistant.apple.com](http://feedbackassistant.apple.com). SE-0296 authors did not answer, so 🤞

---

<div class="post-metadata">

**Author:** ![atierian](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/atierian/32/20271_2.png) [@atierian](https://forums.swift.org/u/atierian)\
**Post date:** [November 2, 2021, 4:23pm UTC](https://forums.swift.org/t/expected-behavior-of-async-overloads-in-protocol-conformance-contexts/53236/5 "2021-11-02T16:23:55Z")

</div>

Interestingly, the compiler's fixit recommendations auto-generate both the sync and async methods.

```swift
struct Bar: P { }
// click `fix` or ctrl + alt + cmd + f 
// produces
struct Bar: P {
  func doSomething() -> String { }
    
  func doSomething() async -> String { }
} 

```

---

<div class="post-metadata">

**Author:** ![Douglas\_Gregor](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/douglas_gregor/32/1362_2.png) [@Douglas\_Gregor](https://forums.swift.org/u/Douglas_Gregor)\
**Post date:** [November 15, 2021, 6:23pm UTC](https://forums.swift.org/t/expected-behavior-of-async-overloads-in-protocol-conformance-contexts/53236/6 "2021-11-15T18:23:55Z")

</div>

> [@gwendal.roue](#):
>
> SE-0296 authors did not answer, so 🤞

This was recently fixed:

[Prefer witnesses with no difference effects to ones that have fewer effects by DougGregor · Pull Request #40088 · apple/swift · GitHub](https://github.com/apple/swift/pull/40088)

> [@gwendal.roue](#):
>
> report a duplicate on [http://feedbackassistant.apple.com](http://feedbackassistant.apple.com)

Please don't. It makes busywork for the people who would rather fix issues.

Doug

---

<div class="post-metadata">

**Author:** ![atierian](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/atierian/32/20271_2.png) [@atierian](https://forums.swift.org/u/atierian)\
**Post date:** [November 16, 2021, 2:21pm UTC](https://forums.swift.org/t/expected-behavior-of-async-overloads-in-protocol-conformance-contexts/53236/7 "2021-11-16T14:21:43Z")

</div>

Thank you for the the fix and for the update here!
