Search code examples
iosswiftasynchronousasync-awaitprotocols

Swift protocol conformance with async overload


Why does this successfully compile and run? (Swift 5.8)

protocol AsyncOverloaded {
    func load()
    func load() async throws
}

class MyClass: AsyncOverloaded {
    func load() {
        print("load called")
    }
}

let myClass = MyClass()
myClass.load()
// prints "load called"

To my understanding MyClass does actually not conform to protocol AsyncOverloaded.

Thanks in advance.


Solution

  • The function load() in MyClass satisfies both protocol requirements.

    A non throwing function satisfies a requirement for a throwing function since it's just like a throwing function that never actually throws. Similarly a synchronous function can satisfy an async requirement.