# \`init(capacity:)\` on RangeReplaceableCollection

**URL:** <https://forums.swift.org/t/init-capacity-on-rangereplaceablecollection/17445>\
**Category:** Pitches\
**Tags:** collection, init, stdlib\
**Created:** [October 28, 2018, 5:21pm UTC](https://forums.swift.org/t/init-capacity-on-rangereplaceablecollection/17445 "2018-10-28T17:21:12Z")\
**Posts on this page:** 1\
**Showing post:** 17

<div class="post-metadata">

**Author:** ![benrimmington](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/benrimmington/32/695_2.png) [@benrimmington](https://forums.swift.org/u/benrimmington)\
**Post date:** [October 29, 2018, 1:59am UTC](https://forums.swift.org/t/init-capacity-on-rangereplaceablecollection/17445/17 "2018-10-29T01:59:43Z")

</div>

I filed [SR-1431](https://bugs.swift.org/browse/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](https://github.com/apple/swift-evolution/blob/master/proposals/0165-dict.md#4-visible-dictionary-capacity) 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](https://github.com/apple/swift-evolution/blob/master/proposals/0223-array-uninitialized-initializer.md#specifying-a-capacity) correctly.

> [@Array Initializer with Access to Uninitialized Buffer](https://forums.swift.org/t/array-initializer-with-access-to-uninitialized-buffer/13689/20):
>
> I'm looping back around to this idea after some discussion of the intention and behavior of array storage allocation in this thread: [Array capacity optimization](https://forums.swift.org/t/array-capacity-optimization/13829). The primary challenge that I've been looking at is that someone calling this method may easily expect that the size of the buffer in the closure matches either the capacity of the array that they observe through the capacity property or the capacity that they reserve immediately before, through a call to reserveCapacity(\_:), but neither…

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

> <https://github.com/apple/swift/blob/a05dd3ed26214a66ce11a84d08d20168b65c5c98/stdlib/public/core/RangeReplaceableCollection.swift#L645-L656>

`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.

---

_[View the full topic](https://forums.swift.org/t/init-capacity-on-rangereplaceablecollection/17445)._
