<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">In Objective-C, I liked that fast enumeration over an NSArray that was nil and one that was empty could be handled with the exact same code:</div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(112, 61, 170);">NSArray</span>&nbsp;*strings =&nbsp;<span class="" style="color: rgb(187, 44, 162);">nil</span>;</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(187, 44, 162);">for</span>&nbsp;(<span class="" style="color: rgb(112, 61, 170);">NSString</span>&nbsp;*string&nbsp;<span class="" style="color: rgb(187, 44, 162);">in</span>&nbsp;strings) {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; …</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">}</div></div><div class=""><br class=""></div><div class="">In Swift, an&nbsp;<span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">Optional</span><span style="font-family: Menlo; font-size: 11px;" class="">&lt;</span><span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">SequenceType</span><span style="font-family: Menlo; font-size: 11px;" class="">&gt;</span>&nbsp;can’t be used this way:</div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(187, 44, 162);">let</span>&nbsp;array: [<span class="" style="color: rgb(112, 61, 170);">AnyObject</span>]? =&nbsp;<span class="" style="color: rgb(187, 44, 162);">nil</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><span class="" style="color: rgb(187, 44, 162);">for</span><span class="" style="color: rgb(0, 0, 0);">&nbsp;object&nbsp;</span><span class="" style="color: rgb(187, 44, 162);">in</span><span class="" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="" style="color: rgb(79, 129, 135);">array</span><span class="" style="color: rgb(0, 0, 0);">&nbsp;{&nbsp;</span>// Compiler error: Value of optional type '[AnyObject]?' not unwrapped; did you mean to use '!' or '?'?</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; …</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">}</div></div><div class=""><br class=""></div><div class="">I know this is a very minor thing and it can be worked around easily by code like this:</div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(187, 44, 162);">for</span>&nbsp;object&nbsp;<span class="" style="color: rgb(187, 44, 162);">in</span>&nbsp;array ?? [] {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; …</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">}</div></div><div class=""><br class=""></div><div class="">… or what I would consider the proper way:</div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(187, 44, 162);">if</span>&nbsp;<span class="" style="color: rgb(187, 44, 162);">let</span>&nbsp;array = array {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp;&nbsp;<span class="" style="color: rgb(187, 44, 162);">for</span>&nbsp;object&nbsp;<span class="" style="color: rgb(187, 44, 162);">in</span>&nbsp;array {</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; &nbsp; &nbsp; …</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; }</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">}</div></div><div class=""><div class=""><br class=""></div><div class="">Handling a sequence that is empty or one that is nil is often the same, at least in my experience. Granted, this points to an API that could be improved to return empty sequences instead of nil in many cases, but that is not always in one’s control. For example in AppKit,&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">NSView</span>’s&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">subviews</span>&nbsp;property is declared as&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">[</span><span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">NSView</span><span style="font-family: Menlo; font-size: 11px;" class="">]</span>, while&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">NSWindow</span>’s&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">childWindows</span>&nbsp;property is&nbsp;<span style="font-family: Menlo; font-size: 11px;" class="">[</span><span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">NSWindow</span><span style="font-family: Menlo; font-size: 11px;" class="">]?</span>, an optional Array.</div><div class=""><br class=""></div></div><div class=""><br class=""></div><div class="">Although I’m not sure this justifies adding a special syntax to Swift, I thought I post this here to see what others think.</div><div class="">I’m proposing a feature that allows iterating over an&nbsp;<span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">Optional</span><span style="font-family: Menlo; font-size: 11px;" class="">&lt;</span><span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">SequenceType</span><span style="font-family: Menlo; font-size: 11px;" class="">&gt;</span>&nbsp;if it is not nil, while doing nothing when it is nil. I think that this should be separate from the normal&nbsp;<span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;" class="">for</span>&nbsp;loop (my first Swift code sample above with the compiler error), but a specialized syntax instead, like the following:</div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(187, 44, 162);">let</span>&nbsp;array: [<span class="" style="color: rgb(112, 61, 170);">AnyObject</span>]? =&nbsp;<span class="" style="color: rgb(187, 44, 162);">nil</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="color: rgb(187, 44, 162);">for</span>&nbsp;object&nbsp;<span class="" style="color: rgb(187, 44, 162);">in?</span>&nbsp;array {&nbsp;<span class="" style="color: rgb(0, 132, 0);">// Note the “in?”</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; …</div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">}</div></div><div class=""><br class=""></div><div class="">With this specialized syntax, you’d be able to see at a glance whether you’re dealing with an&nbsp;<span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">Optional</span>&nbsp;or not. The first Swift code sample in this message would still be a compiler error because it uses “<span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;" class="">for&nbsp;</span><span style="font-family: Menlo; font-size: 11px;" class="">…</span><span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;" class="">&nbsp;in</span>”, but Xcode could show a fix-it that points to the “<span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;" class="">for&nbsp;</span><span style="font-family: Menlo; font-size: 11px;" class="">…</span><span style="color: rgb(187, 44, 162); font-family: Menlo; font-size: 11px;" class="">&nbsp;in?</span>” syntax.</div><div class="">This syntax would not interfere with other proposals discussed on this list, like adding a mandatory ? to identifiers to&nbsp;<span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">Optional</span>s (although this does not imply endorsement on my part).</div><div class=""><br class=""></div><div class="">Also, this is not a proposal to make the&nbsp;<span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">Optional</span>&nbsp;type conform to&nbsp;<span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">SequenceType</span>&nbsp;as was also discussed previously. If that were to be implemented, my proposal would be pointless, but you’d lose the distinction between iterating over an&nbsp;<span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">Optional</span><span style="font-family: Menlo; font-size: 11px;" class="">&lt;</span><span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">SequenceType</span><span style="font-family: Menlo; font-size: 11px;" class="">&gt;</span>&nbsp;and a&nbsp;<span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">SequenceType</span>.&nbsp;Therefore, I’d prefer a specialized syntax to make a clear distinction between iterating over a&nbsp;<span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">SequenceType</span>&nbsp;and an&nbsp;<span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">Optional</span><span style="font-family: Menlo; font-size: 11px;" class="">&lt;</span><span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">SequenceType</span><span style="font-family: Menlo; font-size: 11px;" class="">&gt;</span>.</div><div class=""><br class=""></div><div class="">Cheers,</div><div class=""><br class=""></div><div class="">Marco</div></body></html>