<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="">I’ve also seen unsafeAddressOf(_:) used when interfacing with C function pointers when the lifetime of an object is guaranteed. Many C APIs vend an API like:<div class=""><br class=""></div><div class=""><font face="Menlo" class="">void perform_action(void (*callback)(void *data), void *initial_data);</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class="">For which it is expected to use unsafeAddressOf on a class instance, like:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">perform_action({ data in</font></div><div class=""><font face="Menlo" class=""> let _self = unsafeBitCast(data, to: MyClass.self)</font></div><div class=""><font face="Menlo" class=""> _self.foo()</font></div><div class=""><font face="Menlo" class="">}, data: unsafeAddressOf(self))<br class=""></font><div class=""><br class=""></div><div class="">It’s unsafe and error-prone, sure, but that’s why we have `unsafe` in the name 😅 — I’ve had to use this to interface with libclang.</div><div class=""><br class=""></div><div class="">(For an existing example of this, see SwiftGtk: <a href="https://github.com/TomasLinhart/SwiftGtk/blob/master/Sources/Application.swift" class="">https://github.com/TomasLinhart/SwiftGtk/blob/master/Sources/Application.swift</a>)</div><div class=""><br class=""></div><div class="">— Harlan</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jul 6, 2016, at 11:01 AM, Jacob Bandes-Storch via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote">On Tue, Jul 5, 2016 at 10:50 PM, Dmitri Gribenko via swift-evolution <span dir="ltr" class=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br class="">
> Remove unsafeAddressOf. We are not aware of any real usecases for it. If there are any, it should be renamed to unsafeAddress(of:) to follow the guidelines.<br class="">
Obvious, unless someone comes up with use cases during the review period.<br class="">
<br class=""></blockquote><div class=""><br class=""></div><div class="">It's minor, but I use unsafeAddressOf regularly for writing `description` methods:</div><div class=""><br class=""></div><div class=""> var description: String {</div><div class=""> return "<\(self.dynamicType): \(unsafeAddressOf(self))>{ more info here... }"</div><div class=""> }</div><div class=""><br class=""></div><div class="">I guess this would be covered by some generalized solution for format specifiers in string interpolations, but I gather that won't happen for quite a while...</div></div></div></div>
_______________________________________________<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=""></div></div></body></html>