<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">'x' may not have storage at all, either because it fits entirely in registers or because it's actually a computed property. So this would have to synthesize storage, presumably on the stack. It's unclear when it's safe to destroy that storage, too.</div><div class=""><br class=""></div><div class="">Limiting inout-to-pointer conversions to a single call gives a clear start and end for where the pointer is valid, which allows the compiler to do correct cleanup.</div><div class=""><br class=""></div><div class="">Jordan</div><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 16, 2015, at 14:25 , Jacob Bandes-Storch via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Is there a reason that this couldn't be made to work?<div class=""><p class=""><span class="">&nbsp; &nbsp; let</span><span class=""> ptr: </span><span class="">UnsafePointer</span><span class="">&lt;</span><span class="">Void</span><span class="">&gt; = &amp;x</span></p></div><div class="gmail_extra"><br clear="all" class=""><div class=""><div class="gmail_signature"><div dir="ltr" class=""><div class="">Jacob<br class=""></div></div></div></div>
<br class=""><div class="gmail_quote">On Wed, Dec 16, 2015 at 2:16 PM, Michael Gottesman via swift-evolution <span dir="ltr" class="">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt;</span> wrote:<br class=""><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 class="">
&gt; On Dec 16, 2015, at 4:07 PM, Michael Gottesman via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class="">
&gt;<br class="">
&gt;<br class="">
&gt;&gt; On Dec 16, 2015, at 2:22 PM, Dave Abrahams &lt;<a href="mailto:dabrahams@apple.com" class="">dabrahams@apple.com</a>&gt; wrote:<br class="">
&gt;&gt;<br class="">
&gt;&gt;<br class="">
&gt;&gt;&gt; On Dec 16, 2015, at 11:54 AM, Michael Gottesman via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class="">
&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt; On Dec 16, 2015, at 1:49 PM, Kevin Ballard via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class="">
&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt; 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 class="">
&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt; func foo(ptr: UnsafePointer&lt;Int&gt;) {<br class="">
&gt;&gt;&gt;&gt; // ...<br class="">
&gt;&gt;&gt;&gt; }<br class="">
&gt;&gt;&gt;&gt; foo(&amp;x)<br class="">
&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt; -Kevin<br class="">
&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt; On Wed, Dec 16, 2015, at 11:38 AM, Kevin Ballard wrote:<br class="">
&gt;&gt;&gt;&gt;&gt; # Introduction<br class="">
&gt;&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt;&gt; 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 class="">
&gt;&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt;&gt; # Problem<br class="">
&gt;&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt;&gt; withUnsafePointer() can only be used with mutable variables, because those are the only things that can be used with inout &amp;refs. Both functions are also fairly useless, as &amp;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 &amp;x refs directly to the functions taking a pointer doesn't already fulfill.<br class="">
&gt;&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt;&gt; # Solution<br class="">
&gt;&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt;&gt; Remove the functions from the stdlib. The Swift Book should also be updated to talk about passing an &amp;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 &amp;x ref directly. If any can't, they could be replaced with the following equivalent expressions:<br class="">
&gt;&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt;&gt; { (ptr: UnsafePointer&lt;Int&gt;) in<br class="">
&gt;&gt;&gt;&gt;&gt; // ...<br class="">
&gt;&gt;&gt;&gt;&gt; }(&amp;x)<br class="">
&gt;&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt;&gt; or:<br class="">
&gt;&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt;&gt; withExtendedLifetime(&amp;x) { (ptr: UnsafePointer&lt;Int&gt;) in<br class="">
&gt;&gt;&gt;&gt;&gt; // ...<br class="">
&gt;&gt;&gt;&gt;&gt; }<br class="">
&gt;&gt;&gt;<br class="">
&gt;&gt;&gt; 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 class="">
&gt;&gt;&gt;<br class="">
&gt;&gt;&gt; My fear about withExtendedLifetime is that the name is a misnomer. You are not extending the lifetime.<br class="">
&gt;&gt;<br class="">
&gt;&gt; What makes you say that?<br class="">
&gt;<br class="">
&gt; Let me be more specific.<br class="">
&gt;<br class="">
&gt; My issue with the name 'withExtendedLifetime' is that it is suggestive that the lifetime of &amp;x is being extended in a way that is different from if one just passed off &amp;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 class="">
<br class="">
</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 class="">
<br class="">
with(&amp;x) { (ptr: UnsafePointer&lt;Int&gt;) in<br class="">
&nbsp; &nbsp;...<br class="">
}<br class="">
<br class="">
Michael<br class="">
<div class="HOEnZb"><div class="h5"><br class="">
&gt;<br class="">
&gt; Michael<br class="">
&gt;<br class="">
&gt;&gt;<br class="">
&gt;&gt; If in fact it is true, shouldn't you file a bug report?<br class="">
&gt;&gt;<br class="">
&gt;&gt; -Dave<br class="">
&gt;&gt;<br class="">
&gt;&gt;<br class="">
&gt;&gt;<br class="">
&gt;<br class="">
&gt; _______________________________________________<br class="">
&gt; swift-evolution mailing list<br class="">
&gt; <a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">
&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
<br class="">
_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</div></div></blockquote></div><br class=""></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=ZEz4qHYnXhPr3bBPu-2FxP4tN3HfWKL-2FtJpqkQ0gkOVSD8kDN33-2BGH5zTnOL6ib7efL9Xdc-2FjuFuI2kHeo5nfx-2B7sh4bBzY-2FpvgHuqjapVCbHsMHmi0BGPsZ10uMD-2F1t43HauUN8XA1kYtejDO64ZO9vfUpsMQaYUqiMGWKDXUrbtDV4MXJPmr2W1QOk78-2BmFHdeMytjPIL6yKhgyldgXp0A6PFIYYEH9Q5MKa0D6covI-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>