[swift-evolution] Proposal: [stdlib] Remove withUnsafe[Mutable]Pointer[s]()

Kevin Ballard kevin at sb.org
Wed Dec 16 16:40:40 CST 2015


On Wed, Dec 16, 2015, at 02:25 PM, Jacob Bandes-Storch via swift-evolution wrote:
> Is there a reason that this couldn't be made to work?    let ptr:
> UnsafePointer<Void> = &x


Yes. That breaks several parts of the language:

1. Swift semantics allow you to use computed properties and stored
   properties interchangeably. That expression there can't work with
   computed properties unless Swift silently creates a temporary that
   lives for the entire scope, which would be surprising behavior.
2. Swift also aggressively discards values that are no longer used even
   if they're still in scope (at least under optimization; it likely
   keeps them around in debug builds so they can be inspected). But
   there's no way for Swift to know how long that pointer is going to be
   used for (it could track that variable itself, but what about derived
   values?). So any value that's used in that expression would also have
   to have its lifetime extended for the entire scope, which is
   surprising behavior.
3. The pointer definitely cannot be valid outside of the current scope,
   but there's nothing in that statement to imply this.
   withUnsafePointer() taking a scope tells the user that the pointer is
   only valid for the scope (otherwise it would just return the
   pointer), and similarly the current &x behavior can only be used as a
   parameter to a function, which tells the user that it's only valid
   for the duration of that function call.

-Kevin Ballard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151216/83291c0c/attachment.html>


More information about the swift-evolution mailing list