[swift-evolution] [Review] SE-0107: UnsafeRawPointer API (initialize:with:)

Andrew Trick atrick at apple.com
Tue Jul 5 09:09:48 CDT 2016


> On Jul 5, 2016, at 5:26 AM, Anton Zhilin <antonyzhilin at gmail.com> wrote:
> 
> func initialize<T>(_: T.Type, to: T, count: Int = 1)
>     -> UnsafeMutablePointer<T>
> 
> I wonder why the first parameter is needed. If one is passing literals, it's always more Swift'y to use 'as'.


From the proposal:

---
The type parameter `T` passed to `initialize` is an explicit argument
because the user must reason about the type's size and alignment at
the point of initialization. Inferring the type from the value passed
to the second argument could result in miscompilation if the inferred
type ever deviates from the user's original expectations.
—

You can think of this as the “raw” API. With the latest proposal, it will be more common for users to initialize with a typed pointer anyway:

ptrToA.initialize(to: A())

-Andy


More information about the swift-evolution mailing list