In Swift we can write extensions on generic items such as sequence:
extension Sequence where Iterator.Element : ObservableType {
}
This would guarantee the extension only applies to sequences of (in this case) RxSwift observables.
However if the element constraint is another generic can you then constrain that generic? e.g.:
extension Sequence where Iterator.Element : ObservableType where E : MyType {
}
In the above pseudo code (that does not work) the intent is to say:
This extension should apply to sequences of Observable where the Observable is an Observable of type MyType e.g. [Observable]
You can restrict Iterator.Element
to types conforming to
ObservableType
and then add another constraint for the associated type E
of Iterator.Element
:
protocol ObservableType {
associatedtype E
// ...
}
class MyType { }
extension Sequence where Iterator.Element: ObservableType, Iterator.Element.E: MyType {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With