簡體   English   中英

Swift - 基於 associatedtype 的協議方法簽名

[英]Swift - protocol method signature based on associatedtype

是否在 Swift 中,就像在 TypeScript 中一樣,可以定義一個方法依賴於泛型的結構?

protocol UseCase {
  associatedtype Request
  associatedtype Response
  func execute(controller: () throws -> Request) async throws -> Response
}

如果RequestVoid ,協議應該返回不同的方法簽名。

func execute() async throws -> Response

我會寫一個協議擴展, where Request == Void

extension UseCase where Request == Void {
    func execute() async throws -> Response {
        try await execute {}
    }
}

RequestVoid時,您仍然可以調用execute(controller:) ,但也可以調用無參數的execute()

當您遵守協議時,您需要實現execute(controller:) ,但這不是一個大問題,因為您可以忽略該參數。

您的合同將是謊言,違反了Liskov 替代原則 您可以使用下划線和默認參數繞過它,但我建議不要這樣做。

另外,我認為您想要Never ,而不是Void這就是 Apple 所做的

struct JuiceCase: UseCase {
  func execute(
    controller _: () throws -> Never = { fatalError() }
  ) async throws -> String {
    "🧃"
  }
}


我試圖弄清楚 Cristik 在評論中所說的內容,但不明白。 我在這里看到的是JuiceCase在語義上不符合UseCase ,即使它在語法上也是如此。 我對“可替代性”的解釋是兩者都必須是正確的——即說JuiceCaseUseCase是一個謊言,盡管編譯器目前沒有機制來驗證這一點。

let juiceCase = JuiceCase()
try await juiceCase.execute() // "🧃"
let useCase: some UseCase = juiceCase
try await useCase.execute { fatalError() } // "🧃"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM