[swift-users] Compact iteration of optional collection?
Saagar Jha
saagarjha28 at gmail.com
Thu Jul 28 17:14:25 CDT 2016
The nil check and creating an empty array have very similar performance, in my naïve testing.
Saagar Jha
> On Jul 28, 2016, at 14:59, Jacob Bandes-Storch via swift-users <swift-users at swift.org> wrote:
>
> You should test it out — I'd guess there's a good chance it gets optimized out.
> On Thu, Jul 28, 2016 at 2:58 PM Rick Mann <rmann at latencyzero.com <mailto:rmann at latencyzero.com>> wrote:
> Yeah, I suppose that works. Feels a bit clunky, like the language lacks specific support for this (in that it provides specific support for so many other common constructs). But I guess I can make do with that.
>
> I suppose there's a bit of a performance hit, in that constructing an empty array and iterating over it is more expensive than a simple nil check, but that's unlikely to cause issues in practice.
>
> Thanks.
>
> > On Jul 28, 2016, at 14:56 , Jacob Bandes-Storch <jtbandes at gmail.com <mailto:jtbandes at gmail.com>> wrote:
> >
> > How about "for item in someOptionalContainer ?? []" ?
> >
> > On Thu, Jul 28, 2016 at 2:55 PM, Rick Mann via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
> > I often call methods that return an optional collection. I then iterate over it. The problem is, it's a bit cumbersome to write:
> >
> > if let container = someOptionalContainer
> > {
> > for item in container
> > {
> > }
> > }
> >
> > I wish I could just write
> >
> > for item in someOptionalContainer
> > {
> > }
> >
> > such that if the optional is nil, it just skips the iteration altogether.
> >
> > Is there a syntax for that (especially in Swift 3)?
> >
> >
> > --
> > Rick Mann
> > rmann at latencyzero.com <mailto:rmann at latencyzero.com>
> >
> >
> > _______________________________________________
> > swift-users mailing list
> > swift-users at swift.org <mailto:swift-users at swift.org>
> > https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
> >
>
>
> --
> Rick Mann
> rmann at latencyzero.com <mailto:rmann at latencyzero.com>
>
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160728/e1ad0fc3/attachment.html>
More information about the swift-users
mailing list