`init(capacity:)` on RangeReplaceableCollection

I filed SR-1431 (with a confusing title) a few months before the Swift 3.0 release, to suggest making the following APIs standard:

  • var capacity: Int { get }
  • init(minimumCapacity: Int)
  • mutating func removeAll(keepingCapacity: Bool = false)
  • mutating func reserveCapacity(_ minimumCapacity: Int)

Since then, SE-0165 added the missing APIs to Dictionary and Set.

Using init(capacity:) rather than init(minimumCapacity:) could lead to false assumptions, if I understand the pitch for SE-0223 correctly.

RangeReplaceableCollection has a default implementation of reserveCapacity(_:) which does nothing.

Foundation.Data only has this no-op reserveCapacity(_:) AFAICT. There's an existing Data.init(capacity:) which could be deprecated and renamed.

Should a get-only capacity property also be added, to RangeReplaceableCollection and/or concrete collection types? A default implementation could return the count, if that makes sense.

4 Likes