<div dir="ltr">Hello Dave,<div><br></div><div>I like this change and think that it will help clarify the purpose of the type. As I was reading, the only concern that I had was the name. Could you please provide some of the names that you all have considered so that we can avoid suggesting the same things? My suggestion is </div><div><br></div><div><div style="font-size:13px">    <font face="Menlo">CF<i>Something</i>(<i>arguments…</i>).retainedObject() // when the result is returned at +1</font></div><div style="font-size:13px"><br></div><div style="font-size:13px">or</div><div style="font-size:13px"><br></div><div style="font-size:13px">    <font face="Menlo">CF<i>Something</i>(<i>arguments…</i>).unretainedObject()    // when the result is returned at +0</font></div></div><div style="font-size:13px"><font face="Menlo"><br></font></div>on the premise that the important bit of information is whether or not the object is already retained. No matter what names are chosen, that is the data which determines which method to call. `retainedObject | unretainedObject`, `takeRetainedObject | takeUnretainedObject`, or  `retained | unretained` all seem like viable options (that you have probably considered).<div><div><br></div><div>TJ</div><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Dec 17, 2015 at 8:37 PM, Dave Abrahams via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>Hi Everybody,</div><div><br></div><div>We’ve been working on a rewrite of the Unmanaged&lt;T&gt; component, and are soliciting comments.  First, a little background:</div><div><br></div><div><ul><li><a href="https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html#//apple_ref/doc/uid/TP40014216-CH6-ID79" target="_blank">Unmanaged</a> is primarily used as a return type from imported CoreFoundation functions that haven’t been annotated with reference-counting semantic information</li><li>A secondary known use-case is as a vehicle for creating a COpaquePointer containing a reference’s bits, e.g. for when you need to pass a reference through C APIs that use “void*” as a universal “give me some info and I’ll give it back to your callback” mechanism.</li></ul><div><br></div><ul><li>We saw several problems with Unmanaged that we wanted to fix:</li><ul><li>It was poorly-named (the reference is managed by <i>somebody</i>, we just aren&#39;t representing that management in the type system).</li><li>Its interface was much broader than it needs to be to cover the use-cases</li><li>The purpose of many of its APIs was unclear</li><li>Its documentation was vague and hard to understand.</li><li>It didn’t establish a maximally-safe usage pattern for handling the results of un-annotated CoreFoundation functions.</li></ul></ul></div><div><br></div><div>The code for the proposed replacement, called UnsafeReference, is <a href="https://github.com/dabrahams/swift/blob/6eb86b48d150342709da3f3be9c738df23382866/stdlib/public/core/UnsafeReference.swift" target="_blank">here</a>, and a commit that updates Swift to use it is <a href="https://github.com/dabrahams/swift/commit/6eb86b48d150342709da3f3be9c738df23382866" target="_blank">here</a>. </div><div><br></div><div><font size="5">Maximally Safe Usage</font></div><div><br></div><div>The recommended usage pattern for handling an <font face="Menlo">UnsafeReference&lt;T&gt;</font> returned by a function <font face="Menlo">CF<i>Something</i></font> is to always use the <font face="Menlo">T</font> instance produced by one of the forms:</div><div><br></div><div>    <font face="Menlo">CF<i>Something</i>(<i>arguments…</i>).release() // when the result is returned at +1</font></div><div><br></div><div>or</div><div><br></div><div><div>    <font face="Menlo">CF<i>Something</i>(<i>arguments…</i>).object    // when the result is returned at +0</font></div></div><div><br></div><div>In other words, turn the <span style="font-family:Menlo">UnsafeReference&lt;T&gt;</span> into a safe <span style="font-family:Menlo">T</span> as quickly as possible, and never store the <span style="font-family:Menlo">UnsafeReference&lt;T&gt;</span> in a variable so that it can’t be (mis)used thereafter.</div><div><br></div><div><div><font size="5">Points of Discussion</font></div></div><div><br></div><div>We’re interested in any feedback you might have, but there are a few points we’d especially like to address:</div><div><br></div><div><ul><li>The name of the <font face="Menlo">release()</font> method has been contentious.</li><ul><li>👍: Documentation—or naming conventions such as the “<a href="https://developer.apple.com/library/mac/documentation/CoreFoundation/Conceptual/CFMemoryMgmt/Concepts/Ownership.html" target="_blank">create rule</a>”—normally says something like “you are responsible for releasing the result” in those cases where <font face="Menlo">release()</font> must be called, so there’s a very direct way to know which variant of the recommended usage pattern to employ.</li><li>👎: Some people who are very familiar with existing manual retain/release programming find the recommended usage pattern really counter-intuitive because they&#39;re “using something after calling release on it,” which one never does in Objective-C.</li><li>The alternative names we’ve been able to think of so far are verbose, clumsy, and don’t match up with anything in the documentation of the called function, so this seems like a really hard naming problem.  Better ideas from the community would be most welcome here.</li></ul><li>We’re not sure about the <a href="https://github.com/dabrahams/swift/blob/6eb86b48d150342709da3f3be9c738df23382866/stdlib/public/core/UnsafeReference.swift#L27" target="_blank">terminology</a> (Unretained/Retained/Released) used to precisely describe the semantics of <font face="Menlo">UnsafeReference</font>. We’d like to know if these terms make sense to you or whether you have better ideas.</li><li>We want to know whether the usage pattern recommended above works for you.</li><li>We want to know if the API is sufficiently broad or if there are things you currently get—and need—from <font face="Menlo">Unmanaged</font> that we’ve left out.</li></ul></div><div><br></div><div>Thanks in advance,</div><div><br></div><div><div>
-Dave<div><br></div><br>

</div>
<br></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=3FGfocPbgxkNkmje7djckg9Iw-2BGYY3X6RxJ1tkUXKCqykc-2FqvD6RA4Kk-2FqmXE0Z5aP9e7T8v6GNiR4gmT-2Bsb2uF-2F-2Bjnpc6za8Ziq0Ab54tqerszr6L6jONxR9lePuurF73ErW2FlVUsavvg650rR01qquqQ-2BPSKy9mQhdPVlp5R5P-2FYV5LfMSxcKV-2F2LQsjudDhAsZQswduZViik6Epfu8NUpbkQ-2B3ewHkCNsa-2FWTrM-3D" alt="" width="1" height="1" border="0" style="min-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">
</div>
<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></blockquote></div><br></div>