# SE-0263 (review #2): Add a String Initializer with Access to Uninitialized Storage

**URL:** <https://forums.swift.org/t/se-0263-review-2-add-a-string-initializer-with-access-to-uninitialized-storage/29663>\
**Category:** Proposal Reviews\
**Created:** [October 9, 2019, 2:03pm UTC](https://forums.swift.org/t/se-0263-review-2-add-a-string-initializer-with-access-to-uninitialized-storage/29663 "2019-10-09T14:03:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tkremenek](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tkremenek/32/9115_2.png) [@tkremenek](https://forums.swift.org/u/tkremenek)\
**Post date:** [October 9, 2019, 2:03pm UTC](https://forums.swift.org/t/se-0263-review-2-add-a-string-initializer-with-access-to-uninitialized-storage/29663/1 "2019-10-09T14:03:37Z")

</div>

The [requested revisions](https://forums.swift.org/t/se-0263-add-a-string-initializer-with-access-to-uninitialized-storage/27417/32) to SE-0263 have been made to the proposal:

> [@SE-0263: Add a String Initializer with Access to Uninitialized Storage](https://forums.swift.org/t/se-0263-add-a-string-initializer-with-access-to-uninitialized-storage/27417/32):
>
> The proposal has been accepted, with the request that the proposal author revises the API name to include "unsafe". This was brought up during the review discussion, and the Core Team agreed this was an important aspect to include in the API's name.

Please see the updated review proposal:

> <https://github.com/apple/swift-evolution/blob/main/proposals/0263-string-uninitialized-initializer.md>

The review will resume and continue until October 14 to allow the community to have a chance to discuss the API change.

---

<div class="post-metadata">

**Author:** ![lukasa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lukasa/32/1232_2.png) [@lukasa](https://forums.swift.org/u/lukasa)\
**Post date:** [October 9, 2019, 3:53pm UTC](https://forums.swift.org/t/se-0263-review-2-add-a-string-initializer-with-access-to-uninitialized-storage/29663/2 "2019-10-09T15:53:08Z")

</div>

+1. This addresses my concerns from the previous pitch thread, and I am now happy with this proposal. Thanks so much @David_Smith, and everyone who helped on the original review thread.

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [October 9, 2019, 4:06pm UTC](https://forums.swift.org/t/se-0263-review-2-add-a-string-initializer-with-access-to-uninitialized-storage/29663/3 "2019-10-09T16:06:55Z")

</div>

+1

This nit remains from the previous version of the proposal:

> The new initializer takes a closure that operates on an `UnsafeMutableBufferPointer` and an `inout` count of initialized elements.

No it doesn’t.

Also, I’d very much appreciate if we could back-deploy this API to older versions of the standard library. Doing so would result in cleaner, less spaghetti-like code for users of this initialiser. iOS 13/Catalina just shipped, so it’ll be years before we can raise our minimum deployment targets above Swift 5.1.

As for that back-deployed version, I think we probably _can_ do better than what the user would do manually. Even if for some reason we can't provide the closure with a pointer to `_StringGuts`'s internal buffer, we could do something like:

- If the requested capacity is small enough for a small-string, do a stack allocation like `Int/Float` already do and give a pointer to that.
- If the requested capacity is large, allocate an object and use the "shared Strings" feature (which is part of the ABI already) to defer the copy from initialisation to if/when the String gets mutated.

Worst case, we can just allocate the memory and copy it in to the String buffer, like the user would have to do themselves, but with better usability because on newer systems it would just be faster.

---

<div class="post-metadata">

**Author:** ![johannesweiss](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/johannesweiss/32/38183_2.png) [@johannesweiss](https://forums.swift.org/u/johannesweiss)\
**Post date:** [October 9, 2019, 5:53pm UTC](https://forums.swift.org/t/se-0263-review-2-add-a-string-initializer-with-access-to-uninitialized-storage/29663/4 "2019-10-09T17:53:09Z")

</div>

+1, definitely needed

---

<div class="post-metadata">

**Author:** ![jawbroken](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@jawbroken](https://forums.swift.org/u/jawbroken)\
**Post date:** [October 10, 2019, 3:28am UTC](https://forums.swift.org/t/se-0263-review-2-add-a-string-initializer-with-access-to-uninitialized-storage/29663/5 "2019-10-10T03:28:59Z")

</div>

I said, the previous review thread,

> [@SE-0263: Add a String Initializer with Access to Uninitialized Storage](https://forums.swift.org/t/se-0263-add-a-string-initializer-with-access-to-uninitialized-storage/27417/4):
>
> The similar function for Array uses “unsafeUninitializedCapacity” as the argument label, so this proposal should probably match that or explain why the label should be different.

and I guess you chose the former. I'm glad that the label is now consistent and see no other issues here. Thanks for everyone's work on this.

---

<div class="post-metadata">

**Author:** ![tkremenek](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tkremenek/32/9115_2.png) [@tkremenek](https://forums.swift.org/u/tkremenek)\
**Post date:** [October 10, 2019, 6:01am UTC](https://forums.swift.org/t/se-0263-review-2-add-a-string-initializer-with-access-to-uninitialized-storage/29663/6 "2019-10-10T06:01:08Z")

</div>

> [@Karl](#):
>
> This nit remains from the previous version of the proposal:
> 
> > The new initializer takes a closure that operates on an `UnsafeMutableBufferPointer` and an `inout` count of initialized elements.
> 
> No it doesn’t.

@David_Smith: was this missed in the update?

---

<div class="post-metadata">

**Author:** ![David\_Smith](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_smith/32/37508_2.png) [@David\_Smith](https://forums.swift.org/u/David_Smith)\
**Post date:** [October 10, 2019, 6:23am UTC](https://forums.swift.org/t/se-0263-review-2-add-a-string-initializer-with-access-to-uninitialized-storage/29663/7 "2019-10-10T06:23:09Z")

</div>

> [@tkremenek](#):
>
> `inout` count of initialized elements.

Looks like it, which is odd because I definitely remember making that edit; I suppose the change must not have saved before I committed. In the interests of expediency, I redid the change and pushed it directly to the s-e repo. The new description is

> The new initializer takes a closure that operates on an  
> `UnsafeMutableBufferPointer` referencing the uninitialized contents of the newly created  
> String's storage, and returns a count of initialized elements or 0.

(edit)

Thanks for catching the oversight, @Karl !

---

<div class="post-metadata">

**Author:** ![tkremenek](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tkremenek/32/9115_2.png) [@tkremenek](https://forums.swift.org/u/tkremenek)\
**Post date:** [October 21, 2019, 7:46pm UTC](https://forums.swift.org/t/se-0263-review-2-add-a-string-initializer-with-access-to-uninitialized-storage/29663/8 "2019-10-21T19:46:09Z")

</div>

# Proposal Accepted

This proposal has been accepted with the proposed changes.

Thank you to everyone who participated in the review of SE-0263!

---

<div class="post-metadata">

**Author:** ![tkremenek](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tkremenek/32/9115_2.png) [@tkremenek](https://forums.swift.org/u/tkremenek)\
**Post date:** [October 21, 2019, 7:46pm UTC](https://forums.swift.org/t/se-0263-review-2-add-a-string-initializer-with-access-to-uninitialized-storage/29663/9 "2019-10-21T19:46:20Z")

</div>

Closed on October 21, 2019, 7:46pm UTC


