Skip to content

Commit 79a1a8e

Browse files
committed
[SE-0485] add constructor pattern to alternatives considered
1 parent ea04202 commit 79a1a8e

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

proposals/0485-outputspan.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,23 @@ The additions described in this proposal require a new version of the Swift stan
735735

736736
`OutputSpan` changes the number of initialized elements in a container (or collection), and this requires some operation to update the container after the `OutputSpan` is consumed. Let's call that update operation a "cleanup" operation. The cleanup operation needs to be scheduled in some way. We could associate the cleanup with the `deinit` of `OutputSpan`, or the `deinit` of a wrapper of `OutputSpan`. Neither of these seem appealing; the mechanisms would involve an arbitrary closure executed at `deinit` time, or having to write a full wrapper for each type that vends an `OutputSpan`. We could potentially schedule the cleanup operation as part of a coroutine accessor, but these are not productized yet. The pattern established by closure-taking API is well established, and that pattern fits the needs of `OutputSpan` well.
737737

738+
#### Container construction pattern
739+
740+
A constrained version of possible `OutputSpan` use consists of in-place container initialization. This proposal introduces a few initializers in this vein, such as `Array.init(capacity:initializingWith:)`, which rely on closure to establish a scope. A different approach would be to use intermediate types to perform such operations:
741+
742+
```swift
743+
struct ArrayConstructor<Element>: ~Copyable {
744+
@_lifetime(&self) mutating var outputSpan: OutputSpan<Element>
745+
private let _ArrayBuffer<Element>
746+
747+
init(capacity: Int)
748+
}
749+
750+
extension Array<Element> {
751+
init(_: consuming ArrayConstructor<Element>)
752+
}
753+
```
754+
738755
## <a name="directions"></a>Future directions
739756

740757
#### Helpers to initialize memory in an arbitrary order
@@ -835,7 +852,6 @@ let array = Array(capacity: buffer.count*2) {
835852
}
836853
```
837854

838-
839855
## Acknowledgements
840856

841857
Thanks to Karoy Lorentey, Nate Cook and Tony Parker for their feedback.

0 commit comments

Comments
 (0)