Swift's filter method is defined as follows:
func filter(includeElement: (T) -> Bool) -> [T]
Why does filter definition in Swift's array does not have <T> in its definition (i.e. filter(...))?
Swift's filter method is defined as follows:
func filter(includeElement: (T) -> Bool) -> [T]
Why does filter definition in Swift's array does not have <T> in its definition (i.e. filter(...))?
filter is a method of the Array<T> class, so T is specified at class level and there is no need to replicate that in the method - actually doing that at method level is a mistake:
struct Array<T> ... {
func filter<V>(includeElement: (V) -> Bool) -> [V]
}
because V is a different type that has no relationship with T (unless you set constraints in the generic definition). By mistake I mean T and V are different types, whereas it could be thought they are the same. Having a generic class with a generic method is perfectly legit though.