sách gpt4 ăn đã đi

swift - 嵌套通用约束 : Constrain the T of a generic item inside a generic sequence extension that is constrained to that generic type

In lại 作者:搜寻专家 更新时间:2023-10-31 08:04:19 31 4
mua khóa gpt4 giày nike

在 Swift 中,我们可以对序列等通用项编写扩展:

extension Sequence where Iterator.Element : ObservableType {

}

这将保证扩展仅适用于(在本例中)RxSwift observables 的序列。

但是,如果元素约束是另一个泛型,那么您能否约束该泛型?例如:

extension Sequence where Iterator.Element : ObservableType where E : MyType {

}

在上面的伪代码(不起作用)中,意图是说:

此扩展应该适用于 Observable 的序列,其中 Observable 是类型为 MyType 的 Observable,例如[可观察]

câu trả lời hay nhất

您可以将 Iterator.Element 限制为符合以下条件的类型ObservableType 然后为Iterator.Element 的关联类型E 添加另一个约束:

protocol ObservableType {
associatedtype E
// ...
}

class MyType { }

extension Sequence where Iterator.Element: ObservableType, Iterator.Element.E: MyType {

}

关于swift - 嵌套通用约束 : Constrain the T of a generic item inside a generic sequence extension that is constrained to that generic type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41700697/

31 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress