2 years ago

#59695

test-img

Pierre Mardon

Swift "Ambiguous use of" when adding a neutral generic parameter

When I write:

public func perform() async throws -> Int {
   fatalError()
}

public func perform<Output: Decodable>() async throws -> Output {
    fatalError()
}

public func doIt() async throws -> Int {
    try await perform()
}

Everything is ok.

But when I write:

public func perform<Input: Encodable>(body: Input) async throws -> Int {
   fatalError()
}

public func perform<Input: Encodable, Output: Decodable>(body: Input) async throws -> Output {
    fatalError()
}

public func doIt<Input: Encodable>(body: Input) async throws -> Int {
    try await perform(body: body)
}

Then I've got the "Ambiguous use of 'perform(body:)'" error on the try await perform(body: body) call.

I'm trying to understand why this is. I get that Int conforms to Decodable and that may cause the ambiguity. But then I wonder why in my first example that doesn't happen. I suspect that the introduction of another generic type make the election of candidate functions behave differently, even if that type isn't involved in the ambiguity at all.

Is that so ? As a bonus: could anyone point out an entry point to Swift doc / source code involved in this selection process?

Note: I'm not looking for workarounds, already got this addressed, I'm just trying to understand the underlying mechanisms.

swift

generics

overloading

swift5

0 Answers

Your Answer

Accepted video resources