I don't think:<div><br></div><div><font size="2"><span style="background-color:rgba(255,255,255,0)">let a: [Int]? = [1,2,3]<br><br>for x in a {<br> // a is [1,2,3], not the expected iteration of 1 through 3<br>}</span></font><br><div> </div><div>Is confusing because the type inside the loop is array. You would quickly pick that you needed two loops:</div><div><font size="2"><span style="background-color:rgba(255,255,255,0)"><br>for xs in a {<br> for x in xs {<br> // process x: Int<br> <span></span>}<br>}</span></font><br><br>On Sunday, 13 March 2016, Kevin Lundberg via swift-evolution <<a href="javascript:_e(%7B%7D,'cvml','swift-evolution@swift.org');" target="_blank">swift-evolution@swift.org</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
> +1 -- I've used the "an optional is a collection with max 1 element" rhetoric to explain map/flatMap, would be great if this was true instead of "morally true".<br>
<br>
If optional were a sequence or collection, then could if let be considered a special case for this?:<br>
<br>
for x in optionalX {<br>
//...<br>
}<br>
<br>
I do like the idea that optional could be considered a collection, but I also worry about confusion in this case:<br>
<br>
let a: [Int]? = [1,2,3]<br>
<br>
for x in a {<br>
// a is [1,2,3], not the expected iteration of 1 through 3<br>
}<br>
<br>
Today this does not compile and you are forced to unwrap the optional somehow. With this change I can see that someone may unexpectedly try this and be confused as to why the type of x is not what they expect.<br>
<br>
>> @Dave,Yes, CollectionType would be better than SequenceType.<br>
>><br>
>> On Saturday, 12 March 2016, Dave via swift-evolution<<a>swift-evolution@swift.org</a>(mailto:<a>swift-evolution@swift.org</a>)>wrote:<br>
>> > +1.<br>
>> ><br>
>> > I’d go so far as to suggest that maybe Optional should conform to CollectionProtocol, since you can pretty easily think of an Optional as a collection that can’t have more than one element.<br>
>> ><br>
>> > - Dave Sweeris<br>
>> ><br>
>> > > On Mar 10, 2016, at 7:32 PM, Erica Sadun via swift-evolution<<a>swift-evolution@swift.org</a>(javascript:_e(%7B%7D,'cvml','<a>swift-evolution@swift.org</a>');)>wrote:<br>
>> > ><br>
>> > > While I'm not convinced of the utility or lack thereof, wouldn't it make more sense<br>
>> > > to extend `forEach` to support optionals in a similar way that map and flapMap<br>
>> > > currently do rather than introduce `ifPresent`?<br>
>> > ><br>
>> > > lety:Int? =1<br>
>> > > letn:Int? =nil<br>
>> > ><br>
>> > > _=y.map({print($0) }) // works in current Swift<br>
>> > > _=n.map({print($0) }) // ditto<br>
>> > ><br>
>> > ><br>
>> > > y.forEach{ print($0) } // not currently a thing<br>
>> > > n.forEach{ print($0) } // ditto<br>
>> > ><br>
>> > > Just spitballing here.<br>
>> > ><br>
>> > > -- E<br>
>> > ><br>
>> > > _______________________________________________<br>
>> > > swift-evolution mailing list<br>
>> > > <a>swift-evolution@swift.org</a>(javascript:_e(%7B%7D,'cvml','<a>swift-evolution@swift.org</a>');)<br>
>> > > <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
>> --<br>
>> -- Howard.<br>
> _______________________________________________<br>
> swift-evolution mailing list<br>
> <a>swift-evolution@swift.org</a><br>
> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a>swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div>
</div><br><br>-- <br>-- Howard.<br>