<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jun 24, 2016, at 11:22 AM, Andrew Trick 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 class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><blockquote type="cite" class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: 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-size-adjust: auto; -webkit-text-stroke-width: 0px;"><div class="">On Jun 24, 2016, at 11:17 AM, L. Mihalkovic <<a href="mailto:laurent.mihalkovic@gmail.com" class="">laurent.mihalkovic@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><span class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: 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;">I like the watch-what-you-wish-for warning of unsafeCast.</span><br class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: 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;"></div></blockquote><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: 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=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: 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="">I’ll try porting stdlib to the “UnsafeRawPointer.unsafeCast(to: T.Type)” syntax and see how bad it is.</div></div></div></div></blockquote></div><div class=""><div class=""><br class=""></div><div class="">I don't think there's a clear winner here. Let me enumerate some</div><div class="">options.</div><div class=""><br class=""></div><div class="">Option (1) UnsafePointer<T>(cast: UnsafeRawPointer)</div><div class=""><br class=""></div><div class="">Option (2) UnsafePointer<T>(_: UnsafeRawPointer, to: T.self)</div><div class=""><br class=""></div><div class="">Option (3) UnsafeRawPointer.unsafeCast<T>(to: T.Type) -> UnsafePointer<T></div><div class=""><br class=""></div><div class="">Option (4) unsafeCast(rawPointer: UnsafeRawPointer, to: T.self) -> UnsafePointer<T></div><div class=""><br class=""></div><div class="">---</div><div class="">Option (3) is the most explicit and searchable, and forces</div><div class="">UnsafeRawPointer to be spelled out in the conversion (unless you</div><div class="">already have a raw pointer). I like this because conceptually, you</div><div class="">need to cast to a raw pointer before casting to a new pointee</div><div class="">type, and casting a raw pointer to a typed pointer carries important</div><div class="">semantics beyond simply converting to a typed pointer. The main problem</div><div class="">with Option (3) is that optional raw pointers can't be converted</div><div class="">naturally (without using `map`).</div><div class=""><br class=""></div><div class="">Another thing I'm a little nervous about is confusing a cast of the</div><div class="">pointer value with a cast of the pointee type:</div><div class=""><br class=""></div><div class=""> `unsafeBitCast(rawPtr, to: Int.self)`</div><div class=""><br class=""></div><div class="">is very different from</div><div class=""><br class=""></div><div class=""> `rawPtr.unsafeCast(to: Int.self)`</div><div class=""><br class=""></div><div class="">Does this need to be clarified? If so, we can go back to the</div><div class="">`toPointee` label that I proposed earlier.</div><div class=""><br class=""></div><div class="">With that in mind, Option(4) is starting to look pretty good.</div><div class=""><br class=""></div><div class="">Examples:</div><div class=""><br class=""></div><div class="">---</div><div class="">Case 1: casting a raw pointer as an argument</div><div class=""><br class=""></div><div class="">func foo(_: UnsafePointer<A>)</div><div class=""><br class=""></div><div class="">let rawPtr = UnsafeRawPointer(...)</div><div class=""><br class=""></div><div class="">(1) foo(UnsafePointer(cast: rawPtr))</div><div class=""><br class=""></div><div class="">(2) foo(UnsafePointer(rawPtr, to: A.self))</div><div class=""><br class=""></div><div class="">(3) foo(rawPtr.unsafeCast(to: A.self))</div><div class=""><br class=""></div><div class="">(4) foo(unsafeCast(rawPointer: rawPtr, to: A.self))</div><div class=""><br class=""></div><div class="">---</div><div class="">Case 2: "recasting" a typed pointer argument</div><div class=""><br class=""></div><div class="">Note that typed pointer arguments are implicitly cast to raw pointer</div><div class="">arguments, so the conversion from PtrB to raw is implicit.</div><div class=""><br class=""></div><div class="">func foo(_: UnsafePointer<A>)</div><div class=""><br class=""></div><div class="">let ptrB = UnsafePointer<B>(...)</div><div class=""><br class=""></div><div class="">(1) foo(UnsafePointer(cast: ptrB))</div><div class=""><br class=""></div><div class="">(2) foo(UnsafePointer(ptrB, to: A.self))</div><div class=""><br class=""></div><div class="">(3) foo(UnsafeRawPointer(ptrB).unsafeCast(to: A.self))</div><div class=""><br class=""></div><div class="">(4) foo(unsafeCast(rawPointer: ptrB, to: A.self))</div><div class=""><br class=""></div><div class="">---</div><div class="">Case 3: Optional argument (only Option 3 is affected)</div><div class=""><br class=""></div><div class="">func nullableFoo(_: UnsafePointer<Int>?)</div><div class=""><br class=""></div><div class="">let ptrB: UnsafePointer<UInt>? = ...</div><div class=""><br class=""></div><div class="">(1) nullableFoo(UnsafePointer(cast: ptrB))</div><div class=""><br class=""></div><div class="">(2) nullableFoo(UnsafePointer(ptrB, to: A.self))</div><div class=""><br class=""></div><div class="">(3) nullableFoo(UnsafeRawPointer(ptrB).map { $0.unsafeCast(to: A.self) })</div><div class=""><br class=""></div><div class="">(4) nullableFoo(unsafeCast(rawPointer: ptrB, to: A.self))</div><div class=""><br class=""></div><div class="">---</div><div class="">Case 4: Return values</div><div class=""><br class=""></div><div class="">func foo() -> UnsafePointer<A></div><div class=""><br class=""></div><div class="">func caller() -> UnsafePointer<B> { ...</div><div class=""><br class=""></div><div class="">(1) return UnsafePointer(cast: foo())</div><div class=""><br class=""></div><div class="">(2) return UnsafePointer(foo(), to: B.self)</div><div class=""><br class=""></div><div class="">(3) let rawPtr = UnsafeRawPointer(foo())</div><div class=""> return rawPtr.unsafeCast(to: B.self)</div><div class=""><br class=""></div><div class="">(4) return unsafeCast(rawPointer: foo(), to: B.self)</div></div><div class=""><br class=""></div><div class="">-Andy</div></body></html>