<div dir="ltr"><div><div>While I understand that there is a rationale behind "NonEmptyArray", I don't think that this applies to the original question of Chris.<br>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.<br><br></div>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.<br><br></div>Nicolas<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Feb 1, 2017 at 10:46 PM, Charlie Monroe <span dir="ltr"><<a href="mailto:charlie@charliemonroe.net" target="_blank">charlie@charliemonroe.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">+1<br>
<br>
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...<br>
<div class="HOEnZb"><div class="h5"><br>
> On Feb 1, 2017, at 8:38 PM, Sean Heber via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br>
><br>
> I usually use guard (or sometimes if) and an early return:<br>
><br>
> func run() {<br>
> guard !names.isEmpty else {<br>
> /* stuff */<br>
> return<br>
> }<br>
><br>
> /* stuff with names */<br>
> }<br>
><br>
> l8r<br>
> Sean<br>
><br>
><br>
><br>
>> On Feb 1, 2017, at 12:18 PM, Nicolas Fezans via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br>
>><br>
>> I tend to write this kind of treatment the other way around...<br>
>><br>
>> if names.isEmpty {<br>
>> // do whatever<br>
>> } // on other cases I might have a few else-if to treat other cases that need special treament<br>
>> else {<br>
>> for name in names {<br>
>> // do your thing<br>
>> }<br>
>> }<br>
>><br>
>><br>
>> Nicolas Fezans<br>
>><br>
>><br>
>><br>
>> On Wed, Feb 1, 2017 at 6:31 PM, Saagar Jha via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br>
>> If you’re fine with a couple extra characters, you can use .isEmpty:<br>
>><br>
>> for name in names {<br>
>> // do your thing<br>
>> }<br>
>> if names.isEmpty {<br>
>> // do whatever<br>
>> }<br>
>><br>
>> It’s a bit more typing, but I feel it makes your intentions more clear.<br>
>><br>
>> Saagar Jha<br>
>><br>
>>> On Feb 1, 2017, at 8:48 AM, Chris Davis via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br>
>>><br>
>>> Hi,<br>
>>><br>
>>> Often when I’m programming I stumble upon this scenario:<br>
>>><br>
>>> 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:<br>
>>><br>
>>> class Example_1<br>
>>> {<br>
>>> let names = ["Chris", "John", "Jordan"]<br>
>>><br>
>>> /// Loop over names, if no names, print no names<br>
>>> func run()<br>
>>> {<br>
>>> for name in names<br>
>>> {<br>
>>> print(name)<br>
>>> }<br>
>>><br>
>>> if names.count == 0<br>
>>> {<br>
>>> print("no names")<br>
>>> }<br>
>>> }<br>
>>> }<br>
>>><br>
>>> let exampleOne = Example_1()<br>
>>> exampleOne.run()<br>
>>><br>
>>> However, Personally, I would find it more pleasing to write something like this:<br>
>>><br>
>>> class Example_2_Proposed<br>
>>> {<br>
>>> let names:[String] = []<br>
>>><br>
>>> /// Loop over names, if no names, print no names<br>
>>> func run()<br>
>>> {<br>
>>> for name in names<br>
>>> {<br>
>>> print(name)<br>
>>> } else {<br>
>>> print("no names")<br>
>>> }<br>
>>> }<br>
>>> }<br>
>>><br>
>>> let exampleTwo = Example_2_Proposed()<br>
>>> exampleTwo.run()<br>
>>><br>
>>> 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.<br>
>>><br>
>>> What would be the pros/cons of introducing such syntax?<br>
>>><br>
>>> Is there’s a way of doing something similar in swift already?<br>
>>><br>
>>> Thanks<br>
>>><br>
>>> Chris<br>
>>><br>
>>><br>
>>><br>
>>><br>
>>> ______________________________<wbr>_________________<br>
>>> swift-evolution mailing list<br>
>>> <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
>>> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
>><br>
>><br>
>> ______________________________<wbr>_________________<br>
>> swift-evolution mailing list<br>
>> <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
>> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
>><br>
>><br>
>> ______________________________<wbr>_________________<br>
>> swift-evolution mailing list<br>
>> <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
>> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
><br>
> ______________________________<wbr>_________________<br>
> swift-evolution mailing list<br>
> <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
<br>
</div></div></blockquote></div><br></div>