[swift-evolution] Proposal: [stdlib] Remove withUnsafe[Mutable]Pointer[s]()

Michael Gottesman mgottesman at apple.com
Wed Dec 16 13:57:38 CST 2015


> On Dec 16, 2015, at 1:54 PM, Michael Gottesman via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> 
>> On Dec 16, 2015, at 1:49 PM, Kevin Ballard via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> Another replacement for withUnsafe[Mutable]Pointer is declaring a nested function of the appropriate type (this is equivalent to the anonymous closure, but perhaps more readable):
>> 
>> func foo(ptr: UnsafePointer<Int>) {
>>   // ...
>> }
>> foo(&x)
>> 
>> -Kevin
>> 
>> On Wed, Dec 16, 2015, at 11:38 AM, Kevin Ballard wrote:
>>> # Introduction
>>> 
>>> The stdlib provides functions withUnsafePointer() and withUnsafeMutablePointer() (and plural variants) that take an inout reference and call a block with the UnsafePointer/UnsafeMutablePointer created from the reference.
>>> 
>>> # Problem
>>> 
>>> withUnsafePointer() can only be used with mutable variables, because those are the only things that can be used with inout &refs. Both functions are also fairly useless, as &x refs can be passed directly to functions taking an UnsafePointer or UnsafeMutablePointer. The existence of the functions mostly just causes people to think they're necessary when they're not. The provide no functionality that passing &x refs directly to the functions taking a pointer doesn't already fulfill.
>>> 
>>> # Solution
>>> 
>>> Remove the functions from the stdlib. The Swift Book should also be updated to talk about passing an &x ref to a function that takes an UnsafePointer or UnsafeMutablePointer (but of course changes to the book are not covered by the open-source project). Most uses of these functions can probably be replaced with a &x ref directly. If any can't, they could be replaced with the following equivalent expressions:
>>> 
>>> { (ptr: UnsafePointer<Int>) in
>>>   // ...
>>> }(&x)
>>> 
>>> or:
>>> 
>>> withExtendedLifetime(&x) { (ptr: UnsafePointer<Int>) in
>>>   // ...
>>> }
> 
> One thing to keep in mind here is that with*Pointer and friends is also meant to enable one to work around issues with the optimizer if they come up in a convenient manner. I.e. imagine if one is attempting to process an image using a 5d array and for whatever reason, you are not getting the performance you need. Hopefully you would file a bug report and then use with*Pointer for your image processing loop.
> 
> My fear about withExtendedLifetime is that the name is a misnomer. You are not extending the lifetime.

It sounds to me like what you really want is something like:

with (&x) {

}

which has been spoken about before internally. I do not remember what the result of it is.

> 
>>> 
>>> # Detailed Solution
>>> 
>>> The functions would be marked as unavailable with a message saying to pass &x directly to the function taking a pointer. If it's feasible to do so, Fix-Its would be introduced for the trivial case of the pointer being used once in the closure as a parameter to a function.
>>> 
>>> The documentation of UnsafePointer and UnsafeMutablePointer would be edited to include a mention of how &x refs can be passed to functions expecting an UnsafePointer or UnsafeMutablePointer. This way anyone new to the language who encounters those types in the wild will be able to easily see how to create one from a variable.
>>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151216/c7632ac5/attachment.html>


More information about the swift-evolution mailing list