[swift-evolution] [Review] SE-0174: Change `filter` to return an associated type

Nevin Brackett-Rozinsky nevin.brackettrozinsky at gmail.com
Mon May 1 19:02:38 CDT 2017

Another possibility is to make “map” generic on the return type, something

extension Collection {
    func map<T: RangeReplaceableCollection> (transform: (Iterator.Element)
throws -> T.Iterator.Element) rethrows -> T {
        var result = T()
        for e in self { try result.append(transform(e)) }
        return result

That way the user can choose what type they want. And since there is also a
more-specific implementation returning an Array, that is what you’ll get if
context does not constrain the type, so existing code will still work the

We could do the same for “filter”, in which case the current proposal would
just change what the default type is. So…what I’m talking about here would
be purely additive and can happen later.

In any case, I do like the idea being proposed in SE–0174. If I have a
collection and I filter it down, it makes sense to still be the same kind
of collection. So, +1 from me.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170501/a9cc58e4/attachment.html>

More information about the swift-evolution mailing list