【发布时间】:2020-09-10 01:56:16
【问题描述】:
我不明白为什么这不能编译。如果我从 P 类型中删除 where 限制,它会这样做。
import Combine
protocol Foo {
associatedtype P: Publisher where P.Output == Int
var publisher: P { get }
}
struct Bar: Foo {
var publisher: some Publisher {
Just(1)
}
}
错误提示Type 'Bar' does not conform to protocol 'Foo'。我想这是因为publisher 返回类型不只是任何some Publisher。但是在 SwiftUI 中,View 使用了类似的方法,只是它对 View 类型没有限制。
有什么方法可以编译这段代码吗?
【问题讨论】: