[swift-users] [swift-evolution] for-else syntax

Howard Lovatt howard.lovatt at gmail.com
Fri Feb 3 19:15:43 CST 2017


Why not add to the library:

extension Sequence {
    func forEach(_ eacher: (Iterator.Element) throws -> Void, elser: ()
throws -> Void) rethrows {
        var hasIterated = false
        for element in self {
            hasIterated = true
            try eacher(element)
        }
        guard hasIterated else {
            try elser()
            return
        }
    }
}

On Fri, 3 Feb 2017 at 8:33 pm, Jeremy Pereira via swift-users <
swift-users at swift.org> wrote:

>
> > On 2 Feb 2017, at 20:12, Erica Sadun via swift-users <
> swift-users at swift.org> wrote:
> >
> > I've taken this over to Swift Users from Swift Evolution.
> >
> > I think you'd want to handle the exceptional case first,
>
> Although it is the way I would do it for an array, sometimes you don’t
> know that a sequence is empty until after you have iterated it.
>
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
-- 
-- Howard.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170204/539870ed/attachment.html>


More information about the swift-users mailing list