[swift-evolution] Why doesn't removeLast() on Collection return an optional?

Louis D'hauwe louisdhauwe at silverfox.be
Mon Oct 17 16:19:40 CDT 2016


I see, the really issue I'm trying to bring up is that removeLast() can easily fail (and result in a fatal error).
And unless you read the documentation, that's not very obvious.

Maybe another approach would be to make removeLast() throw an exception if the collection is empty?
That way you can use "try" to safely remove the last item of a collection.

The reasoning behind this is that right now, to safely use removeLast(), you pretty much have to do something like this:
if !collection.isEmpty {
	collection.removeLast()
}

Having this method throw would allow to change the above to:
try collection.removeLast()

– Louis D'hauwe

> On 17 Oct 2016, at 22:52, Max Moiseev <moiseev at apple.com> wrote:
> 
> You can use popLast, that does exactly that: https://github.com/apple/swift/blob/master/stdlib/public/core/BidirectionalCollection.swift#L213 <https://github.com/apple/swift/blob/master/stdlib/public/core/BidirectionalCollection.swift#L213>
> 
> Max
> 
>> On Oct 17, 2016, at 1:14 PM, Louis D'hauwe via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> Regarding the removeLast() function on Collection:
>> 
>> The current implementation <https://github.com/apple/swift/blob/c3b7709a7c4789f1ad7249d357f69509fb8be731/stdlib/public/core/BidirectionalCollection.swift#L228> is:
>> @discardableResult
>> public mutating func removeLast() -> Iterator.Element {
>>  	let element = last!
>> 	self = self[startIndex..<index(before: endIndex)]
>> 	return element
>> }
>> 
>> This makes it so that if you call removeLast() on an empty collection you get a fatal error.
>> ("fatal error: can't remove last element from an empty collection")
>> 
>> The documentation for removeLast() <https://github.com/apple/swift/blob/c3b7709a7c4789f1ad7249d357f69509fb8be731/stdlib/public/core/BidirectionalCollection.swift#L220> even has this noted:
>> "The collection must not be empty.".
>> 
>> Surely you could just remove the explicit unwrapping of 'last' and add a guard statement? 
>> 
>> As such:
>> @discardableResult
>> public mutating func removeLast() -> Iterator.Element? {
>>  	guard let element = last else {
>> 		return nil
>> 	}
>> 	self = self[startIndex..<index(before: endIndex)]
>> 	return element
>> }
>> 
>> 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.
>> 
>> – Louis D'hauwe
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161017/7db7577c/attachment.html>


More information about the swift-evolution mailing list