<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">There's still the "release" issue (std::unique_ptr::release versus -[NSObject release]), but "transfer" seems like a good word to me. What about "transferByRetaining" and "transferWithoutRetaining"?</div><br class=""><div class="">
<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Lucida Grande'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; ">Félix</span>
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">Le 20 déc. 2015 à 00:01:22, Nevin Brackett-Rozinsky via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> a écrit :</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" style="font-family: LucidaGrande; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Floating an idea here—not sure if it’s even in the right ballpark, and I’m certainly not tied to the specific wording, but what about something along the lines of:<div class=""><br class=""></div><div class="">.transferByReleasing()</div><div class="">.transferWithoutReleasing() // or perhaps just .transfer()</div><div class=""><br class=""></div><div class="">Or the slightly-more-verbose:</div><div class=""><br class=""></div><div class="">.transferObjectByReleasingReference()</div><div class="">.transferObjectWithoutReleasingReference() // or .transferObject()</div><div class=""><br class=""></div><div class="">Nevin</div><div class=""><br class=""></div></div><div class="gmail_extra" style="font-family: LucidaGrande; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""><div class="gmail_quote">On Sat, Dec 19, 2015 at 11:37 PM, Jacob Bandes-Storch via swift-evolution<span class="Apple-converted-space"> </span><span dir="ltr" class=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></span><span class="Apple-converted-space"> </span>wrote:<br class=""><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div dir="ltr" class="">Hi Dave,<div class="">Thanks for sharing the proposal. I finally had a chance to catch up with the discussion.</div><div class=""><br class=""></div><div class="">Generally I like the proposal and it would serve my needs well.</div><div class=""><br class=""></div><div class="">I share others' concern that completely manual retain/release calls should still be allowed in some way. This doesn't have to be via UnsafeReference, but I don't think it makes sense to replace Unmanaged with UnsafeReference as proposed without some solution to this.</div><div class=""><br class=""></div><div class="">Regarding naming:</div><span class=""><div class=""><br class=""></div><div class="">> problems with Unmanaged that we wanted to fix:</div><div class="">> It was poorly-named (the reference is managed by somebody, we just aren't representing that management in the type system).</div><div class=""><br class=""></div></span><div class="">I don't really agree with this. The management isn't "not represented" — the Unmanaged type explicitly says, at least to me, "this particular reference to the object does not manage its lifetime". So I don't think UnsafeReference is particularly an improvement here, though I'm not against it.</div><div class=""> </div><div class="">- The release() name makes sense to me; it behaves like removeFirst/removeAtIndex. I'd also suggest something like consumeReference(). And for manual retains, if that's in scope for this API, you could use addReference().</div><div class=""><br class=""></div><div class="">Regarding COpaquePointer:</div><div class=""><br class=""></div><div class="">- Just to clarify, it looks like the proposed API would produce usage patterns like:</div><div class=""><br class=""></div><div class=""> <span class="Apple-converted-space"> </span>someCFunction(context: COpaquePointer(UnsafeReference(retaining: self)))<br class=""></div><div class=""><br class=""></div><div class=""> <span class="Apple-converted-space"> </span>func myCallback(context: COpaquePointer) {</div><div class=""> <span class="Apple-converted-space"> </span>let object = UnsafeReference<Foo>(bitPattern: context).object<br class=""> <span class="Apple-converted-space"> </span>}</div><div class=""><br class=""></div><div class=""> <span class="Apple-converted-space"> </span>func myCleanup(context: COpaquePointer) {<br class=""></div><div class=""> <span class="Apple-converted-space"> </span>UnsafeReference<Foo>(bitPattern: context).release()</div><div class=""> <span class="Apple-converted-space"> </span>}</div><div class=""><div class=""><br class=""></div><div class="">- I'm curious why you chose a COpaquePointer initializer, rather than toOpaque() as the current Unmanaged API provides? On my UnsafePointer+Unmanaged proposal, you<span class="Apple-converted-space"> </span><a href="https://github.com/apple/swift-evolution/pull/44#issuecomment-165902471" target="_blank" class="">commented</a><span class="Apple-converted-space"> </span>"we can’t give just UnsafePointer<Void> an init taking an UnsafeReference". But (a) it sounds like this is an eventual goal (I started working on it but it's a bit over my head currently); (b) using toOpaque() instead solves this problem:</div></div><div class=""><br class=""></div><div class=""> <span class="Apple-converted-space"> </span>func toOpaque() -> UnsafePointer<Void> {<br class=""> <span class="Apple-converted-space"> </span>return unsafeBitCast(_storage, UnsafePointer<Void>.self)</div><div class=""> <span class="Apple-converted-space"> </span>}</div><div class=""> <span class="Apple-converted-space"> </span>...</div><div class=""> <span class="Apple-converted-space"> </span>someCFunction(context: UnsafeReference(retaining: self).toOpaque())</div><div class=""><br class=""></div><div class="">My motivation here is mostly that C void* APIs are currently bridged as UnsafePointer<Void>, so APIs that produce COpaquePointer are an obstacle to using them. If the trend is away from UnsafePointer and toward better "Opaque" semantics, that's fine with me; I'd just like the API to be easy to use in the meantime.</div><div class=""><br class=""></div><div class="">In the same comment you also said "pointers to Void and incomplete types are not in any sense “unsafe” (once you restrict the interface as appropriate for incomplete types), and so maybe we want OpaquePointer<T> for these". It seems to me, though, that anything which can operate on arbitrary memory addresses is indeed Unsafe.</div><div class=""><br class=""></div><div class="">Regarding documentation comments:</div><div class=""><br class=""></div><div class="">- It might be worth mentioning that init(retaining:) and init(withoutRetaining:) are most likely to be used before conversion to an opaque pointer, rather than immediately calling .object or .release() which wouldn't be a very useful pattern.</div><div class=""><br class=""></div><div class="">- A weakness I see is that CF/other C APIs won't have comments saying which "state" the returned UnsafeReference is in. The UnsafeReference doc comments are very clear about which operations may be used when, but the user is forced to mentally translate "…responsible for releasing…" into "this reference is in the *retained* state" — a possible source of confusion, unless all the CF doc comments can be reworded when they're imported into Swift to be more explicit.</div><div class=""><br class=""></div><div class="">- Very minor: your doc comment on "public var object" has a stray/double `. Similarly, the top-level comment has a stray/double ".<br class=""></div><div class="gmail_extra"><br clear="all" class=""><div class=""><div class=""><div dir="ltr" class=""><div class="">Jacob Bandes-Storch<br class=""></div></div></div></div><div class=""><div class="h5"><br class=""><div class="gmail_quote">On Sat, Dec 19, 2015 at 7:43 PM, Dave Abrahams via swift-evolution<span class="Apple-converted-space"> </span><span dir="ltr" class=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></span><span class="Apple-converted-space"> </span>wrote:<br class=""><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><span class=""><br class="">> On Dec 19, 2015, at 4:22 PM, Brent Royal-Gordon <<a href="mailto:brent@architechies.com" target="_blank" class="">brent@architechies.com</a>> wrote:<br class="">><br class="">>>> Mainly, because simply saying "release" or "released" is a bit ambiguous to me.Are you saying it *has been* released, or are you saying it *needs to be* released?<br class="">>><br class="">>> But nobody proposed "released" as a method name. In what way is "release" ambiguous? It's an imperative verb.<br class="">><br class="">> I guess you're right that "release" is unambiguous, but as you mentioned, it's also strange to release a value and then use it.<br class=""><br class=""></span>Yes. I think there are no really great choices here (at least not so far) so the question is whether that strangeness is enough of a problem to outweigh the points release() has in its favor. What do you think?<br class=""><span class=""><br class="">> I think what I'm trying to get at here is that I prefer to think of the operations on Unmanaged as "explain to ARC how it should handle this object", rather than "do some manual operations so that ARC will do the right thing". Maybe the current Unmanaged design has shown the limitations of that approach, though.<br class=""><br class=""></span>Not at all; the Unmanaged design—at least in my best understanding of its intent—is firmly in the imperative/manual operations camp. I wanted to do something more declarative, but the "I want to manage the reference that I claim was passed to me at +1" operation is side-effectful. Are we really comfortable with hiding that fact?<br class=""><span class=""><br class="">>> But you applied "take" to both of them? One of them is idempotent while the other is not.<br class="">><br class="">> The preferred way to use Unmanaged is that you immediately convert it to a managed reference without ever storing it or using it in any other way. That means you should immediately call either the retain-and-return operation or the don't-retain-and-return operation. Both of these should only ever be called once. You may instead choose to keep the reference Unmanaged and manually retain, release, and access it, but best practices discourage that.<br class=""><br class=""></span>As I said in my original post, I'm ambivalent about the importance of highlighting the distinctions of safety and idempotence between these methods, but even if they're named similarly I don't see any merit in starting with "take." One thing I really dislike about is that the receiver, the UnsafeReference, isn't "taking" anything. The *caller* might be said to be taking something from the UnsafeReference, but only in the "returned at +1" case.<br class=""><br class="">How I see it: along with the UnsafeReference the called CF function either notionally<br class="">a) gives (possibly-shared) ownership of the object directly to the caller, or<br class="">b) gives the caller a token that allows him to get (shared) ownership of the object<br class=""><br class="">In case a), the caller needs to ask the UnsafeReference to transfer (or "release") that ownership into a strong reference, and. In case b), the caller needs to explicitly get (shared) ownership.<br class=""><br class="">If this description doesn't sound right to you, please try to correct it; that may help me understand your perspective better.<br class=""><span class=""><br class="">> Now, one of the preferred, do-only-once operations *happens* to be safe to apply more than once, but I view that as an implementation detail. Both of them *happen* to be implemented in the same way as manual operations (`manuallyRelease()` and `object`), but I view that as an implementation detail, too.<br class=""><br class=""></span>Hm, well, I don't view `object` as a "manual operation" and there's value in having a smaller API surface area. I don't think I want a separate `manuallyRelease` method if there is another method that has the same semantics. One of the greatest weaknesses of the current Unmanaged is that its interface is too broad and hard to grasp.<br class=""><span class=""><br class="">> Honestly, I might be happier splitting an UnsafeReference type out of Unmanaged and putting the manual retain/release stuff into that:<br class=""><br class=""></span>As noted in my original post, I really don't want to keep the name "Unmanaged" for anything. If anything, it's "ManuallyManaged." And I am very wary of API surface area creep here, whether it's in one type or two.<br class=""><span class=""><br class="">> // Unmanaged is a high-level type for moving object references in and out of ARC's control.<br class="">> struct Unmanaged<T: class> {<br class="">> func created() -> T<br class="">> func gotten() -> T<br class="">><br class="">> // Also would have stuff for passing, which I haven't even thought about yet<br class="">> }<br class="">><br class="">> // UnsafeReference is a low-level type for manually managing the retain count of an object.<br class="">> struct UnsafeReference<T: class> {<br class="">> init(_ object: T)<br class="">> init(_ unmanaged: Unmanaged<T>)<br class="">><br class="">> var object: T<br class="">><br class="">> // Some or all of these might return T<br class="">> func retain()<br class="">> func release()<br class="">> func autorelease()<br class="">> }<br class="">><br class="">> This puts the discouraged manual operations off in their own type where they'll be available to those who know about them, but not sitting right there on every unaudited call.<br class="">><br class="">>>> (I kind of want to suggest that retrieving an object through these calls should destroy the reference so it can't be used again, but I don't think that fits with Swift's mutation model without turning `Unmanaged`/`UnsafeReference` into a reference type and adding lots of overhead.)<br class="">>><br class="">>> Yes, there's no way to reconcile that with the safety offered by the recommended usage patterns, since you can't mutate an rvalue.<br class="">><br class="">> I thought so. That's too bad. (I wonder if the compiler can emit warnings instead, though.)<br class=""><br class=""></span>I don't know what you have in mind here.<br class=""><span class=""><br class="">><br class="">>>> (One possibility would be to have a single call with an enum parameter, like `bridge(.Create)` and `bridge(.Get)`. This would let you use the regular form of the verb.)<br class="">>><br class="">>> There's no "bridging" going on here, though. This is simply "turn this unsafe thing into a safe thing in one of two ways"<br class="">><br class="">> The "bridge" here comes from the Objective-C bridging casts, but I think there it's meant to refer to toll-free bridging, which is not what's happening in Swift.<br class="">><br class="">> If the type name remains `Unmanaged`, then perhaps `manage(_:)` would be better? (I don't like `managing` here because that again implies it's side-effect-free and safe to call more than once.)<br class=""><br class=""></span>Well again, we're not asking the receiver, the UnsafeReference, to manage anything. And don't forget, we have two operations and need two names, especially if you want them to feel similar.<br class=""><span class=""><br class="">>> So far, my personal assessment of this direction is that it's no better than what I proposed, and has several weaknesses I'd like to avoid. In fact, it seems very similar to and roughly as understandable as the current Unmanaged design. I recognize that this is a highly subjective judgement, so if others disagree with me, I'd really like to hear about it. This is a tough design space and ultimately, what resonates best with the community is likely to be the best choice.<br class="">><br class="">> I understand. I'm obviously struggling with this too, as you can see from how much I'm changing my design based on your replies, rather than defending the design as suggested before.<br class="">><br class="">> Ultimately, Unmanaged is an API for handling an abstraction failure. That's inherently going to be tricky and subjective.<br class=""><br class=""></span>Yup.<br class=""><span class=""><font color="#888888" class=""><br class="">-Dave<br class=""></font></span><div class=""><div class=""><br class=""><br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" target="_blank" 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></div></div><img alt="" width="1" height="1" border="0" style="min-height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;" class=""><span class="Apple-converted-space"> </span><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=""><br class=""></blockquote></div><br class=""></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=iRI3beHTe3UxYAHTlV3lA38zIPfHMhyuRzgTmGKV6k52NRLN6x8NfXcJXc10NsvmfyMCh-2B7TiHpvxHxAicxYdIheGi4o1tKKXV0nxvuPiepRNKCevEMD-2B4FnVzgXv6gI7hfcswZTz-2FpczniFbPUjlcqlQo4GEUQw4cY0DblTCpAHWA7OxzgJtD3bzZVq17d7eDd2wtdlsgpVTBT7PVP6kBlufBoWxbqPIbL8dCC47VM-3D" alt="" width="1" height="1" border="0" style="font-family: LucidaGrande; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;" class=""><span style="font-family: LucidaGrande; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class=""><span class="Apple-converted-space"> </span>_______________________________________________</span><br style="font-family: LucidaGrande; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: LucidaGrande; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">swift-evolution mailing list</span><br style="font-family: LucidaGrande; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="mailto:swift-evolution@swift.org" style="font-family: LucidaGrande; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">swift-evolution@swift.org</a><br style="font-family: LucidaGrande; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family: LucidaGrande; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></blockquote></div><br class=""></body></html>