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

T.J. Usiyan griotspeak at gmail.com
Mon May 1 19:12:49 CDT 2017


There are many things that I would like more but this is a reasonable

On Mon, May 1, 2017 at 8:02 PM, Nevin Brackett-Rozinsky via swift-evolution
<swift-evolution at swift.org> wrote:

> Another possibility is to make “map” generic on the return type, something
> like:
> 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 same.
> 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.
> Nevin
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170501/60359058/attachment.html>

More information about the swift-evolution mailing list