I have places where I have both Set
and Array
of MyType
.
In these places, I need to filter down my Sequences
, and you'll notice that the filter
block is the same for both Sequence
types.
Is there any way I can implement a generic Sequence
extension, where the filterFor
method will return the correct type (Set
or Array
), depending on the receiver?
extension Set where Element: MyType {
func filterFor(valueToMatch:String) -> Set<MyType> {
return self.filter{
$0.myProperty.caseInsensitiveCompare(valueToMatch) == .orderedSame
}
}
}
extension Array where Element: MyType {
func filterFor(valueToMatch:String) -> [MyType] {
return self.filter{
$0.myProperty.caseInsensitiveCompare(valueToMatch) == .orderedSame
}
}
}