為什么定義為類Self.Failure時AddIndexPublisher會報錯,而定義為結構時卻沒有報錯?
考慮以下內容:(編譯錯誤顯示在相應行上方的注釋中)
class AddIndexPublisher<InputP: Publisher>: Publisher {
typealias Output = (InputP.Output, Int)
typealias Failure = InputP.Failure
let ip: InputP
init(ip: InputP) {
self.ip = ip
}
func receive<S>(subscriber: S)
// 'Failure' is not a member type of type 'Self'
// 'Output' is not a member type of type 'Self'
where S : Subscriber, Self.Failure == S.Failure, Self.Output == S.Input
{
let sub = AddIndexSubscription<
// 'Failure' is not a member type of type 'Self'
S, InputP, Self, InputP.Output, Self.Failure
>(sub: subscriber, ipub: ip, opub: self)
subscriber.receive(subscription: sub)
}
}
洗掉Self.讓編譯錯誤在使用時也消失class。但是,我很好奇為什么產生這些錯誤的結構和類之間存在差異。規范中是否有一節解釋了一些背景?
謝謝。
uj5u.com熱心網友回復:
我的基本想法是 2 點:
與繼承有關,因為結構不能從其他結構繼承。這也連接到第 2 點。
Self.Failure正在使用Self,它是一種型別(在這種情況下,是當前相同的物件型別)。還有Output和Failure是Publisher協議的相關型別。由于類的繼承,使用Self關聯型別沒有意義(這些型別到底是什么Failure?Output)。洗掉Self.告訴編譯器使用此物件中定義的型別別名。
無論如何,這是一個很好的問題,謝謝你:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/528370.html
標籤:迅速仿制药结合
上一篇:通過列舉值參考型別或類?
