<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="">Hi everyone!<br class=""><div class=""><br class=""></div><div class="">I’ve been<a href="https://github.com/adrfer/Sort" class=""> tinkering with a few sorting algorithms in Swift</a>&nbsp;and noticed a diference in behaviour when it comes to accessing the first element of a collection.</div><div class=""><br class=""></div><div class="">If the method `first` from `CollectionType` is used, one needs to unwrap the returned optional to use it. Pretty standard stuff... However, if one uses subscripting, no unwrapping is required.&nbsp;</div><div class=""><br class=""></div><div class="">For example, when using subscripting:</div><div class=""><br class="">func&nbsp;quickSort(array: [Int])&nbsp;-&gt;&nbsp;[Int] {<br class=""><br class="">&nbsp; &nbsp;&nbsp;guard&nbsp;array.count&nbsp;&gt;&nbsp;1&nbsp;else&nbsp;{&nbsp;return&nbsp;array&nbsp;}<br class=""><br class="">&nbsp; &nbsp;&nbsp;let&nbsp;(pivot, rest)&nbsp;=&nbsp;(<b class="">array[0]</b>, array.dropFirst())<b class=""> &nbsp; &nbsp;// no need to unwrap `pivot` for further use</b><br class=""><br class="">&nbsp; &nbsp;&nbsp;let&nbsp;lessThan&nbsp;=&nbsp;rest.filter({&nbsp;$0&nbsp;&lt;&nbsp;<b class="">pivot</b>&nbsp;})<br class=""><br class="">&nbsp; &nbsp;&nbsp;let&nbsp;greaterThanOrEqual&nbsp;=&nbsp;rest.filter({&nbsp;$0&nbsp;&gt;=&nbsp;<b class="">pivot</b>&nbsp;})<br class=""><br class="">&nbsp; &nbsp;&nbsp;return&nbsp;quickSort(lessThan)&nbsp;+&nbsp;[<b class="">pivot</b>]&nbsp;+&nbsp;quickSort(greaterThanOrEqual)<br class="">}</div><div class=""><br class=""></div><div class="">But, when using the method `first`:</div><div class=""><br class=""></div><div class="">func&nbsp;quickSort(array: [Int])&nbsp;-&gt;&nbsp;[Int] {<br class=""><br class="">&nbsp; &nbsp;&nbsp;guard&nbsp;array.count&nbsp;&gt;&nbsp;1&nbsp;else&nbsp;{&nbsp;return&nbsp;array&nbsp;}<br class=""><br class="">&nbsp; &nbsp;&nbsp;let&nbsp;(pivot, rest)&nbsp;=&nbsp;(<b class="">array.first</b>, array.dropFirst())<br class=""><br class="">&nbsp; &nbsp;&nbsp;let&nbsp;lessThan&nbsp;=&nbsp;rest.filter({&nbsp;$0&nbsp;&lt;&nbsp;<b class="">pivot</b>&nbsp;})<b class=""> &nbsp; &nbsp;//&nbsp;unwrapping `pivot` is not required here</b><br class=""><br class="">&nbsp; &nbsp;&nbsp;let&nbsp;greaterThanOrEqual&nbsp;=&nbsp;rest.filter({&nbsp;$0&nbsp;&gt;=&nbsp;<b class="">pivot</b>&nbsp;})<b class=""> &nbsp; &nbsp;// unwrapping `pivot` is not required here</b><br class=""><br class="">&nbsp; &nbsp;&nbsp;return&nbsp;quickSort(lessThan)&nbsp;+&nbsp;[<b class="">pivot!</b>]&nbsp;+&nbsp;quickSort(greaterThanOrEqual)<b class=""> &nbsp; &nbsp;&nbsp;// unwrapping `pivot` is now required here</b><br class="">}</div><div class=""><br class=""></div><div class="">Isn’t the subscript supposed to return an optional as well?</div><div class=""><br class=""></div><div class="">Why unwrapping isn't required when `pivot` is used within the `filter` call?</div><div class=""><br class=""></div><div class="">Best,</div><div class=""><br class=""></div><div class="">— A</div></body></html>