"Ambiguous use of" 访问重载函数时出错
"Ambiguous use of" error in when accessing overloaded function
我有 3 个具有相同名称但不同签名的函数定义如下
// 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)
第 1 和第 2 个声明可以按预期访问,但在第 3 个声明中出现错误 Ambiguous use of 'send'
,第 2 和第 3 个声明可能是候选者。
根据我的理解,这不应该发生,因为第 3 次调用不需要 return,所以它应该调用第 3 次声明。我在这里错过了什么?
Note declaration 2 does not have @discardableResult
您需要告诉编译器 return 类型是什么,因此将调用更改为
try await self.send(httpsRequest) as Void
从 Apple Developer 站点查看 this blog post
我有 3 个具有相同名称但不同签名的函数定义如下
// 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)
第 1 和第 2 个声明可以按预期访问,但在第 3 个声明中出现错误 Ambiguous use of 'send'
,第 2 和第 3 个声明可能是候选者。
根据我的理解,这不应该发生,因为第 3 次调用不需要 return,所以它应该调用第 3 次声明。我在这里错过了什么?
Note declaration 2 does not have
@discardableResult
您需要告诉编译器 return 类型是什么,因此将调用更改为
try await self.send(httpsRequest) as Void
从 Apple Developer 站点查看 this blog post