<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="">Hi Louis,</div><div class=""><br class=""></div><div class="">I believe, sometimes there are situations where you know for sure that your collection is not empty. Maybe you are already in the context where the check has been performed. In these cases there is no reason you’d have to pay the price of an emptiness check once again.</div><div class=""><br class=""></div><div class="">Perhaps, it should be documented better, as in “popLast should generally be preferred since it is safer to use. You should only opt for the removeLast if you are certain the collection is not empty and performance is an issue”.</div><div class=""><br class=""></div><div class="">If you agree, mind sending us a pull request with such a documentation change?</div><div class=""><br class=""></div><div class="">Thanks!</div><div class="">Max</div><div class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">On Oct 17, 2016, at 2:19 PM, Louis D'hauwe &lt;<a href="mailto:louisdhauwe@silverfox.be" class="">louisdhauwe@silverfox.be</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">I see, the really issue I'm trying to bring up is that&nbsp;<span class="" style="color: rgb(69, 69, 69); font-family: 'Helvetica Neue';">removeLast() can easily fail (and result in a fatal error).</span><div class=""><font color="#454545" face="Helvetica Neue" class="">And unless you read the documentation, that's not very obvious.</font></div><div class=""><font color="#454545" face="Helvetica Neue" class=""><br class=""></font></div><div class=""><font color="#454545" face="Helvetica Neue" class="">Maybe another approach would be to make&nbsp;</font><span class="" style="color: rgb(69, 69, 69); font-family: 'Helvetica Neue';">removeLast() throw an exception if the collection is empty?</span></div><div class=""><font color="#454545" face="Helvetica Neue" class="">That way you can use "try" to safely remove the last item of a collection.</font></div><div class=""><br class=""></div><div class=""><font color="#454545" face="Helvetica Neue" class="">The reasoning behind this is that right now, to safely use removeLast(), you pretty much have to do something like this:</font></div><div class=""><font color="#454545" face="Helvetica Neue" class="">if !collection.isEmpty {</font></div><div class=""><font color="#454545" face="Helvetica Neue" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span></font><span class="" style="color: rgb(69, 69, 69); font-family: 'Helvetica Neue';">collection.removeLast()</span></div><div class=""><font color="#454545" face="Helvetica Neue" class="">}</font></div><div class=""><font color="#454545" face="Helvetica Neue" class=""><br class=""></font></div><div class=""><font color="#454545" face="Helvetica Neue" class="">Having this method throw would allow to change the above to:</font></div><div class=""><font color="#454545" face="Helvetica Neue" class="">try&nbsp;</font><span class="" style="color: rgb(69, 69, 69); font-family: 'Helvetica Neue';">collection.removeLast()</span></div><div class=""><div class=""><font color="#454545" face="Helvetica Neue" class=""><br class=""></font></div><div class=""><font color="#454545" face="Helvetica Neue" class="">– Louis D'hauwe</font></div><div class=""><font color="#454545" face="Helvetica Neue" class=""><br class=""></font></div></div><div class=""><blockquote type="cite" class=""><div class="">On 17 Oct 2016, at 22:52, Max Moiseev &lt;<a href="mailto:moiseev@apple.com" class="">moiseev@apple.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">You can use popLast, that does exactly that:&nbsp;<a href="https://github.com/apple/swift/blob/master/stdlib/public/core/BidirectionalCollection.swift#L213" class="">https://github.com/apple/swift/blob/master/stdlib/public/core/BidirectionalCollection.swift#L213</a><div class=""><br class=""></div><div class="">Max</div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Oct 17, 2016, at 1:14 PM, Louis D'hauwe via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class="">Regarding the removeLast() function on Collection:</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""><a href="https://github.com/apple/swift/blob/c3b7709a7c4789f1ad7249d357f69509fb8be731/stdlib/public/core/BidirectionalCollection.swift#L228" class="">The current implementation</a>&nbsp;is:<br class="">
@discardableResult</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class="">public mutating func removeLast() -&gt; Iterator.Element {</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class="">&nbsp;<span class="Apple-tab-span" style="white-space:pre">        </span>let element = last!</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>self = self[startIndex..&lt;index(before: endIndex)]</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>return element</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class="">}</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class="">This makes it so that if you call <b class="">removeLast() </b>on an empty collection you get a fatal error.</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class="">("fatal error: can't remove last element from an empty collection")</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""><a href="https://github.com/apple/swift/blob/c3b7709a7c4789f1ad7249d357f69509fb8be731/stdlib/public/core/BidirectionalCollection.swift#L220" class="">The documentation for <b class="">removeLast()</b></a>&nbsp;even has this noted:<br class="">
"The collection must not be empty.".</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class="">Surely you could just remove the explicit unwrapping of 'last' and add a guard statement?&nbsp;</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class="">As such:</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class="">@discardableResult</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class="">public mutating func removeLast() -&gt; Iterator.Element? {</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class="">&nbsp;<span class="Apple-tab-span" style="white-space:pre">        </span>guard let element = last else {</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>return nil</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>self = self[startIndex..&lt;index(before: endIndex)]</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>return element</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class="">}</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class="">It sure seems more "Swifty" to alert at compile time that removing the last item of a collection might fail, and make it return nil as a result.</div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69);" class="">– Louis D'hauwe</div><div class=""><br class=""></div></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></body></html>