@@ -832,6 +832,7 @@ extension RangeReplaceableCollection where Self : BidirectionalCollection {
832
832
public mutating func removeLast( ) -> Element {
833
833
_precondition ( !isEmpty, " Can't remove last element from an empty collection " )
834
834
// NOTE if you change this implementation, change popLast above as well
835
+ // AND change the tie-breaker implementations in the next extension
835
836
if let result = _customRemoveLast ( ) { return result }
836
837
return remove ( at: index ( before: endIndex) )
837
838
}
@@ -865,10 +866,27 @@ extension RangeReplaceableCollection where Self : BidirectionalCollection {
865
866
}
866
867
}
867
868
868
- // FIXME: swift-3-indexing-model: file a bug for the compiler?
869
869
/// Ambiguity breakers.
870
870
extension RangeReplaceableCollection
871
- where Self : BidirectionalCollection , SubSequence == Self {
871
+ where Self : BidirectionalCollection , SubSequence == Self {
872
+ /// Removes and returns the last element of the collection.
873
+ ///
874
+ /// Calling this method may invalidate all saved indices of this
875
+ /// collection. Do not rely on a previously stored index value after
876
+ /// altering a collection with any operation that can change its length.
877
+ ///
878
+ /// - Returns: The last element of the collection if the collection is not
879
+ /// empty; otherwise, `nil`.
880
+ ///
881
+ /// - Complexity: O(1)
882
+ @_inlineable
883
+ public mutating func popLast( ) -> Element ? {
884
+ if isEmpty { return nil }
885
+ // duplicate of removeLast logic below, to avoid redundant precondition
886
+ if let result = _customRemoveLast ( ) { return result }
887
+ return remove ( at: index ( before: endIndex) )
888
+ }
889
+
872
890
/// Removes and returns the last element of the collection.
873
891
///
874
892
/// The collection must not be empty.
@@ -884,9 +902,8 @@ extension RangeReplaceableCollection
884
902
@discardableResult
885
903
public mutating func removeLast( ) -> Element {
886
904
_precondition ( !isEmpty, " Can't remove last element from an empty collection " )
887
- if let result = _customRemoveLast ( ) {
888
- return result
889
- }
905
+ // NOTE if you change this implementation, change popLast above as well
906
+ if let result = _customRemoveLast ( ) { return result }
890
907
return remove ( at: index ( before: endIndex) )
891
908
}
892
909
0 commit comments