[swift-evolution] for-else syntax

Nicolas Fezans nicolas.fezans at gmail.com
Thu Feb 2 04:54:42 CST 2017


While I understand that there is a rationale behind "NonEmptyArray", I
don't think that this applies to the original question of Chris.
It seems that it is fully acceptable for the array to be empty in the
considered case, but only some different treatment shall happen in that
case.

I could easily imagine that situation when querying a database: you do not
want to specify that the query have no answer, but in one case you might
want to iterate through the items matching you query and do something with
them whereas in the other case you might want to notify the user that there
were no match.

Nicolas

On Wed, Feb 1, 2017 at 10:46 PM, Charlie Monroe <charlie at charliemonroe.net>
wrote:

> +1
>
> This is generally why I've suggested about a week or two ago
> "NonEmptyArray" - an array that ensures it's not empty. Which is IMHO a
> sensible thing to use sometimes instead...
>
> > On Feb 1, 2017, at 8:38 PM, Sean Heber via swift-evolution <
> swift-evolution at swift.org> wrote:
> >
> > I usually use guard (or sometimes if) and an early return:
> >
> > func run() {
> >  guard !names.isEmpty else {
> >    /* stuff */
> >    return
> >  }
> >
> >  /* stuff with names */
> > }
> >
> > l8r
> > Sean
> >
> >
> >
> >> On Feb 1, 2017, at 12:18 PM, Nicolas Fezans via swift-evolution <
> swift-evolution at swift.org> wrote:
> >>
> >> I tend to write this kind of treatment the other way around...
> >>
> >> if names.isEmpty {
> >>      // do whatever
> >> } // on other cases I might have a few else-if to treat other cases
> that need special treament
> >> else {
> >>      for name in names {
> >>              // do your thing
> >>      }
> >> }
> >>
> >>
> >> Nicolas Fezans
> >>
> >>
> >>
> >> On Wed, Feb 1, 2017 at 6:31 PM, Saagar Jha via swift-evolution <
> swift-evolution at swift.org> wrote:
> >> If you’re fine with a couple extra characters, you can use .isEmpty:
> >>
> >> for name in names {
> >>      // do your thing
> >> }
> >> if names.isEmpty {
> >>      // do whatever
> >> }
> >>
> >> It’s a bit more typing, but I feel it makes your intentions more clear.
> >>
> >> Saagar Jha
> >>
> >>> On Feb 1, 2017, at 8:48 AM, Chris Davis via swift-evolution <
> swift-evolution at swift.org> wrote:
> >>>
> >>> Hi,
> >>>
> >>> Often when I’m programming I stumble upon this scenario:
> >>>
> >>> I have a list of items that may or may not be empty - if it’s full, I
> do one thing, if it’s empty I do something else, my code looks like this:
> >>>
> >>> class Example_1
> >>> {
> >>>    let names = ["Chris", "John", "Jordan"]
> >>>
> >>>    /// Loop over names, if no names, print no names
> >>>    func run()
> >>>    {
> >>>        for name in names
> >>>        {
> >>>            print(name)
> >>>        }
> >>>
> >>>        if names.count == 0
> >>>        {
> >>>            print("no names")
> >>>        }
> >>>    }
> >>> }
> >>>
> >>> let exampleOne = Example_1()
> >>> exampleOne.run()
> >>>
> >>> However, Personally, I would find it more pleasing to write something
> like this:
> >>>
> >>> class Example_2_Proposed
> >>> {
> >>>    let names:[String] = []
> >>>
> >>>    /// Loop over names, if no names, print no names
> >>>    func run()
> >>>    {
> >>>        for name in names
> >>>        {
> >>>            print(name)
> >>>        } else {
> >>>            print("no names")
> >>>        }
> >>>    }
> >>> }
> >>>
> >>> let exampleTwo = Example_2_Proposed()
> >>> exampleTwo.run()
> >>>
> >>> The difference here is a “for-else” type syntax where if there were no
> items in the array it would simply fall through to the else statement.
> >>>
> >>> What would be the pros/cons of introducing such syntax?
> >>>
> >>> Is there’s a way of doing something similar in swift already?
> >>>
> >>> Thanks
> >>>
> >>> Chris
> >>>
> >>>
> >>>
> >>>
> >>> _______________________________________________
> >>> swift-evolution mailing list
> >>> swift-evolution at swift.org
> >>> https://lists.swift.org/mailman/listinfo/swift-evolution
> >>
> >>
> >> _______________________________________________
> >> swift-evolution mailing list
> >> swift-evolution at swift.org
> >> https://lists.swift.org/mailman/listinfo/swift-evolution
> >>
> >>
> >> _______________________________________________
> >> swift-evolution mailing list
> >> swift-evolution at swift.org
> >> https://lists.swift.org/mailman/listinfo/swift-evolution
> >
> > _______________________________________________
> > 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/20170202/f9292225/attachment.html>


More information about the swift-evolution mailing list