访问重载函数时"Ambiguous use of"错误



我有三个同名但不同签名的函数,定义如下

// 1
func send<T: Decodable>(_ request: HTTPSClient.Request) async throws -> T {
...
}
// 2
func send(_ request: HTTPSClient.Request) async throws -> Data {
...
}
// 3
func send(_ request: HTTPSClient.Request) async throws {
...
}

当尝试调用这些

// Works fine, SomeResponse is Codable
let response: SomeResponse = try await self.send(httpsRequest)
// Works fine
let response: Data = try await self.send(httpsRequest)
// Does not work
try await self.send(httpsRequest)

第一个和第二个声明可以访问,但在第三个声明上,我得到错误Ambiguous use of 'send',第二个和第三个声明作为可能的候选。

根据我的理解,这不应该发生,因为第三个调用不期望返回,所以它应该调用第三个声明。我遗漏了什么?

说明声明2没有@discardableResult

您需要告诉编译器返回类型是什么,因此将调用更改为

try await self.send(httpsRequest) as Void

查看这篇来自Apple Developer站点的博文

最新更新