[swift-evolution] Pitch: Improved Swift pointers

Dave Abrahams dabrahams at apple.com
Thu Jul 13 13:45:15 CDT 2017

on Wed Jul 12 2017, Taylor Swift <swift-evolution at swift.org> wrote:

> Hi all, I’ve written up a proposal to modify the unsafe pointer API for
> greater consistency, safety, and ease of use.
> ~~~
> Swift currently offers two sets of pointer types — singular pointers such
> as UnsafeMutablePointer, and vector (buffer) pointers such as UnsafeMutable
> *Buffer*Pointer. This implies a natural separation of tasks the two kinds
> of pointers are meant to do. For example, buffer pointers implement
> Collection conformance, while singular pointers do not.
> However, some aspects of the pointer design contradict these implied roles.
> It is possible to allocate an arbitrary number of instances from a type
> method on a singular pointer, but not from a buffer pointer. The result of
> such an operation returns a singular pointer, even though a buffer pointer
> would be more appropriate to capture the information about the *number* of
> instances allocated. It’s possible to subscript into a singular pointer,
> even though they are not real Collections. Some parts of the current design
> turn UnsafePointers into downright *Dangerous*Pointers, leading users to
> believe that they have allocated or freed memory when in fact, they have
> not.
> This proposal seeks to iron out these inconsistencies, and offer a more
> convenient, more sensible, and less bug-prone API for Swift pointers.
> <https://gist.github.com/kelvin13/a9c033193a28b1d4960a89b25fbffb06>

I have no problem with this direction in principle; it sounds like a
good idea.  However, before affirming any particular design I would like
to see the results of any such proposal applied to a fairly large body
of code that uses Unsafe[XXX]Pointer today in diverse ways (such as the
Swift standard library itself), to demonstrate that it does in fact
improve the code in practice.



More information about the swift-evolution mailing list