<div dir="ltr">Is there a reason that this couldn't be made to work?<div>
<p class=""><span class=""> let</span><span class=""> ptr: </span><span class="">UnsafePointer</span><span class=""><</span><span class="">Void</span><span class="">> = &x</span></p></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"><div dir="ltr"><div>Jacob<br></div></div></div></div>
<br><div class="gmail_quote">On Wed, Dec 16, 2015 at 2:16 PM, Michael Gottesman via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5"><br>
> On Dec 16, 2015, at 4:07 PM, Michael Gottesman via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br>
><br>
><br>
>> On Dec 16, 2015, at 2:22 PM, Dave Abrahams <<a href="mailto:dabrahams@apple.com">dabrahams@apple.com</a>> wrote:<br>
>><br>
>><br>
>>> On Dec 16, 2015, at 11:54 AM, Michael Gottesman via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br>
>>><br>
>>>><br>
>>>> On Dec 16, 2015, at 1:49 PM, Kevin Ballard via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br>
>>>><br>
>>>> 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):<br>
>>>><br>
>>>> func foo(ptr: UnsafePointer<Int>) {<br>
>>>> // ...<br>
>>>> }<br>
>>>> foo(&x)<br>
>>>><br>
>>>> -Kevin<br>
>>>><br>
>>>> On Wed, Dec 16, 2015, at 11:38 AM, Kevin Ballard wrote:<br>
>>>>> # Introduction<br>
>>>>><br>
>>>>> 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.<br>
>>>>><br>
>>>>> # Problem<br>
>>>>><br>
>>>>> 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.<br>
>>>>><br>
>>>>> # Solution<br>
>>>>><br>
>>>>> 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:<br>
>>>>><br>
>>>>> { (ptr: UnsafePointer<Int>) in<br>
>>>>> // ...<br>
>>>>> }(&x)<br>
>>>>><br>
>>>>> or:<br>
>>>>><br>
>>>>> withExtendedLifetime(&x) { (ptr: UnsafePointer<Int>) in<br>
>>>>> // ...<br>
>>>>> }<br>
>>><br>
>>> 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.<br>
>>><br>
>>> My fear about withExtendedLifetime is that the name is a misnomer. You are not extending the lifetime.<br>
>><br>
>> What makes you say that?<br>
><br>
> Let me be more specific.<br>
><br>
> My issue with the name 'withExtendedLifetime' is that it is suggestive that the lifetime of &x is being extended in a way that is different from if one just passed off &x to any old function. In reality though, nothing special is happening here implying that the name is misleading. A better name IMO would be something that drops any such implication.<br>
<br>
</div></div>For example, we could just use 'with' (something that has been suggested for this use case in various blog posts). I.e.:<br>
<br>
with(&x) { (ptr: UnsafePointer<Int>) in<br>
...<br>
}<br>
<br>
Michael<br>
<div class="HOEnZb"><div class="h5"><br>
><br>
> Michael<br>
><br>
>><br>
>> If in fact it is true, shouldn't you file a bug report?<br>
>><br>
>> -Dave<br>
>><br>
>><br>
>><br>
><br>
> _______________________________________________<br>
> swift-evolution mailing list<br>
> <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</div></div></blockquote></div><br></div></div>