[swift-evolution] [Discussion] Cleaning up stdlib pointer and buffer routines (Open Issues Affecting Standard Library API Stability)

Charlie Monroe charlie at charliemonroe.net
Fri Jul 8 03:13:27 CDT 2016


> On Jul 8, 2016, at 1:01 AM, Dave Abrahams via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
> on Wed Jul 06 2016, Jacob Bandes-Storch <swift-evolution at swift.org> wrote:
> 
>>> * *Remove unsafeAddressOf*. "We are not aware of any real use cases for
>>> it. If there are any, it should be renamed to unsafeAddress(of:) to follow
>>> the guidelines." (https://bugs.swift.org/browse/SR-1957
>>> rdar://problem/18589289)
>>> 
>>> 
>> Oops, I just responded to this on another thread. Pasting:
>> 
>> It's minor, but I use unsafeAddressOf regularly for writing `description`
>> methods:
>> 
>>    var description: String {
>>        return "<\(self.dynamicType): \(unsafeAddressOf(self))>{ more info
>> here... }"
>>    }
>> 
>> 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...
> 
> If we remove unsafeAddressOf, we have no way to get the address of an
> immutable variable.  A mutable variable can be passed inout to a
> function taking an UnsafePointer, but you can't add & to an immutable
> binding.  
> 
> That's a loss I'm willing to take.  I just wanted to point out exactly
> what we will have no way to accomplish if we remove unsafeAddressOf.

I'm OK with removing it as long as:

- ObjectIdentifier provides a correct description (SR-2014)

- ObjectIdentifer.pointerValue returns just the opaque pointer (i.e. exposes private _value member) since I'm not a big fan of having "ObjectIdentifier" in the logs. There should be an easy way of getting a pointer to an object without using Unmanaged.

- Ideally, the description would include dynamicType of the value + pointer:

ObjectIdentifier(NSObject<0x00101234>)

Which can always be achieved by storing Any.Type within ObjectIdentifier - something I believe can be useful on multiple occasions in the future as well.

I.e. full public API for ObjectIdentifier:

public struct ObjectIdentifier : Hashable, Comparable {
    // Existing:
    public var uintValue: UInt { get }
    public var hashValue: Int { get }
    
    public init(_ x: AnyObject)
    public init(_ x: Any.Type)
    
    // New:
    public var pointerValue: UnsafePointer<Void> { get }
    public var type: Any.Type { get }
}

// New:
extension ObjectIdentifier: CustomStringConvertible {
    public var description: String { get } // return "ObjectIdentifier(\(self.type)<\(self.pointerValue)>)"
}


> 
> -- 
> Dave
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list