我創建了以下代碼,除了 TestDBAPI 的定義外,一切正常。
當我想創建一個符合 DBAPIProtocol 協議的型別時,總是無法生成滿足泛型約束的型別實體
請問,如何定義TestNoteFetcher 來滿足DBAPIProtocol 的協議要求。
ps:希望在DBAPIProtocol中能保持泛型定義的靈活性
謝謝
import Combine
// For Value
public enum WrappedID: Equatable, Identifiable, Sendable, Hashable {
case string(String)
case integer(Int)
public var id: Self {
self
}
}
public protocol BaseValueProtocol: Equatable, Identifiable, Sendable {
var id: WrappedID { get }
}
public struct Note: BaseValueProtocol {
public var id: WrappedID
public var index: Int
public init(id: WrappedID, index: Int) {
self.id = id
self.index = index
}
}
// For Object
public protocol ConvertibleValueObservableObject<Value>: ObservableObject, Equatable, Identifiable where ID == WrappedID {
associatedtype Value: BaseValueProtocol
func convertToValueType() -> Value
}
public final class TestNote: ConvertibleValueObservableObject {
public static func == (lhs: TestNote, rhs: TestNote) -> Bool {
true
}
public var id: WrappedID {
.integer(1)
}
public func convertToValueType() -> Note {
.init(id: .integer(1), index: 0)
}
}
// For Fetcher
public protocol ObjectFetcherProtocol<Object,ConvertValue> {
associatedtype ConvertValue: BaseValueProtocol
associatedtype Object: ConvertibleValueObservableObject<ConvertValue>
var stream: AsyncPublisher<AnyPublisher<[Object], Never>> { get }
}
public final class TestNoteFetcher: ObjectFetcherProtocol {
public typealias ConvertValue = Note
public typealias Object = TestNote
public var stream: AsyncPublisher<AnyPublisher<[TestNote], Never>> {
sender.eraseToAnyPublisher().values
}
public var sender: CurrentValueSubject<[TestNote], Never>
public init(_ notes: [TestNote] = []) {
sender = .init(notes)
}
}
// For API
public protocol DBAPIProtocol {
var notesFetcher: () async -> any ObjectFetcherProtocol<any ConvertibleValueObservableObject<Note>, Note> { get set }
}
// get error in here . Cannot convert value of type 'TestNoteFetcher.Object' (aka 'TestNote') to closure result type 'any ConvertibleValueObservableObject<Note>'
public final class TestDBAPI: DBAPIProtocol {
public var notesFetcher: () async -> any ObjectFetcherProtocol<any ConvertibleValueObservableObject<Note>, Note> = {
TestNoteFetcher([])
}
}

uj5u.com熱心網友回復:
由于您的閉包回傳太多anys,編譯器會感到困惑并告訴您 that TestNoteFetcherdoesn't conform any ObjectFetcherProtocol。泛型是您的朋友,您可以使用它associatedtype來跳過所有這些代碼并解決問題:
public protocol DBAPIProtocol {
associatedtype Fetcher: ObjectFetcherProtocol
var notesFetcher: () async -> Fetcher { get set }
}
public final class TestDBAPI: DBAPIProtocol {
public var notesFetcher: () async -> TestNoteFetcher = {
TestNoteFetcher([])
}
}
uj5u.com熱心網友回復:
有時問題就在我面前,但我確實對此視而不見。感謝您的提醒。我對這部分代碼進行了更改,它現在可以作業了。
public protocol ObjectFetcherProtocol<ConvertValue> {
associatedtype ConvertValue: BaseValueProtocol
var stream: AsyncPublisher<AnyPublisher<[any ConvertibleValueObservableObject<ConvertValue>], Never>> { get }
}
public final class TestNoteFetcher: ObjectFetcherProtocol {
public var stream: AsyncPublisher<AnyPublisher<[any ConvertibleValueObservableObject<Note>], Never>> {
sender.eraseToAnyPublisher().values
}
public var sender: CurrentValueSubject<[any ConvertibleValueObservableObject<Note>], Never>
public init(_ notes: [any ConvertibleValueObservableObject<Note>] = []) {
sender = .init(notes)
}
}
這樣,我仍然可以保持 DBAPIProtocol 中定義的靈活性,而無需引入關聯型別
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/526817.html
標籤:迅速仿制药
上一篇:更新陳述句中的奇怪語法
