[swift-users] LazyCollection.map()

Aaron Bohannon aaron678 at gmail.com
Tue Jun 28 20:07:29 CDT 2016

AH!  I wasn't familiar enough with the LazyCollectionType protocol to
realize that its map() function had a different signature.

So, presumably, if I pass a non-throwing closure, the compiler will choose
the lazy map().  However, it's not immediately obvious to me why it would
be chosen.  Is it because the LazyCollectionType version of map() "shadows"
the CollectionType whenever either one could be chosen (since
LazyCollectionType extends CollectionType)?  Or is it because the function
with the more restrictive argument type is always chosen when more than one
version of the function could match?

On Tue, Jun 28, 2016 at 5:38 PM, Dmitri Gribenko <gribozavr at gmail.com>

> On Tue, Jun 28, 2016 at 3:37 PM, Aaron Bohannon via swift-users
> <swift-users at swift.org> wrote:
> > Does the code below have a well-defined behavior?
> It invokes the eager map() that is available on Array.lazy.
> Array.lazy is a collection, so it has an eager map() from the
> Collection protocol.  The lazy map() does not accept a throwing
> closure, so it does not match and the type checker chooses the eager
> one.
> Arguably, in non-generic context this code should not type check.
> Dmitri
> --
> main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
> (j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160628/31e774f6/attachment.html>

More information about the swift-users mailing list