[英]Swift Type Inference with Generic method
I'm working on an SDK, and have developed a nice concise Combine pipeline method that accepts a generic parameter that's used to decode json with.我正在开发一个 SDK,并开发了一个非常简洁的 Combine 管道方法,该方法接受用于解码 json 的通用参数。 Effectively, it's a re-usable combine pipeline for JSON ->
Decodable
.实际上,它是 JSON ->
Decodable
的可重用组合管道。 Works really well.效果很好。 Here's what that pipeline looks like:
这是管道的样子:
func records<Record: Decodable>(forRequest request:RestRequest ) -> AnyPublisher<[Record], Never> {
return NetworkService.publisher(for: request)
.tryMap({ (response) -> Data in
response.asData()
})
.decode(type: Wrapper<Record>.self, decoder: JSONDecoder())
.map({ (record) -> [Record] in
record.records
})
.catch({ _ in
Just([Record]())
})
.eraseToAnyPublisher()
}
Usage:用法:
contactsCancellable = NetworkService.records(forRequest: request)
.receive(on: RunLoop.main)
.assign(to: \.contacts, on: self)
It's my understanding that Swift+Combine is inferring the generic parameter type from the assign(to:, on:)
call.我的理解是 Swift+Combine 是从
assign(to:, on:)
调用推断泛型参数类型。
But the powers that be want a non-Combine version, and I'm really struggling to figure out how to help Swift infer the type.但是想要一个非组合版本的权力,我真的很努力想弄清楚如何帮助 Swift 推断类型。 I tried building a direct analog like this:
我尝试构建一个像这样的直接模拟:
func fetchRecords<Record: Decodable>(forRequest request: RestRequest,
_ completionBlock: @escaping (Result<[Record], RestClientError>) -> Void) {
RestClient.shared.send(request: request) { result in
switch result {
case .success(let response):
do {
let decoder = JSONDecoder()
let wrapper = try decoder.decode(Wrapper<Record>.self, from: response.asData())
completionBlock(.success(wrapper.records))
} catch {
completionBlock(.success([Record]()))
}
case .failure(let err):
completionBlock(.failure(err))
}
}
}
This compiles however, executing that method like this:然而,这会编译,像这样执行该方法:
NetworkService.fetchRecords(forRequest: request) { records in
print(records)
}
Results in a lovingly cryptic error Generic parameter 'Record' could not be inferred导致一个非常神秘的错误无法推断通用参数“记录”
How can I specify that generic Record 'type' - anything that conforms to Decodable, in this non-combine version?在此非组合版本中,如何指定通用 Record 'type' - 任何符合 Decodable 的内容?
Ps: Here's that Wrapper struct: Ps:这是包装结构:
struct Wrapper<R: Decodable>: Decodable {
var totalSize: Int
var done: Bool
var records: [R]
}
You could specify the generic type in the closure parameter list:您可以在闭包参数列表中指定泛型类型:
NetworkService.fetchRecords(forRequest: request) { (result: Result<[ConcreteRecordType], RestClientError>) {
switch result {
case .success(let records):
// "records" is of type [ConcreteRecordType]
//...
case .failure(let error):
//...
}
}
But that can be cumbersome having to provide the full Result
type in the closure, so I recommend that you fill in the generic type information by accepting it as a parameter.但是在闭包中提供完整的
Result
类型可能很麻烦,所以我建议您通过接受它作为参数来填写泛型类型信息。 (like the Decoder
functions do.) (就像
Decoder
功能一样。)
func fetchRecords<Record: Decodable>(ofType type: Record.Type, forRequest request: RestRequest, _ completionBlock: @escaping (Result<[Record], RestClientError>) -> Void) {
//... same code...
}
Then, you'd call it like this:然后,你会这样称呼它:
NetworkService.fetchRecords(ofType: ConcreteRecordType.self, forRequest: request) { result in
// No need to specify closure argument type :)
switch result {
case .success(let records):
// "records" is of type [ConcreteRecordType]
//...
case .failure(let error):
//...
}
}
Voila!瞧! The explicit type provided to
fetchRecords
cascades down to the closure argument type.提供给
fetchRecords
的显式类型级联到闭包参数类型。 No need to provide the type in the closure parameter list.无需在闭包参数列表中提供类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.