[Pitch] `Int.init(ObjectIdentifier)` and `UInt.init(ObjectIdentifier)` should have a `bitPattern:` label


(Arnold Schwaighofer) #1

Int.init(ObjectIdentifier) and UInt.init(ObjectIdentifier) should have a 'bitPattern:’ label to make it clear at the use site that we interpret the value as a bit pattern.

In Swift we have ObjectIdentifier values which uniquely identify a class instance or metatype. They are implemented as a struct which holds the value of the reference to the instance or metatype as a raw pointer.

  /// A unique identifier for a class instance or metatype.
  public struct ObjectIdentifier : Hashable, Comparable {
    internal let _value: Builtin.RawPointer
    ...
  }

We have constructors for Int and Uint that capture this value. These constructors don’t have an argument label.

  extension UInt {
    /// Create a `UInt` that captures the full value of `objectID`.
    public init(_ objectID: ObjectIdentifier) {
      self.init(Builtin.ptrtoint_Word(objectID._value))
    }
  }

  extension Int {
    /// Create an `Int` that captures the full value of `objectID`.
    public init(_ objectID: ObjectIdentifier) {
      self.init(bitPattern: UInt(objectID))
    }
  }

This proposals suggest adding a label ‘bitPattern’ to the constructor: Int.init(bitPattern: ObjectIdentifier).

  extension UInt {
    /// Create a `UInt` that captures the full value of `objectID`.
    public init(bitPattern objectID: ObjectIdentifier) {
      self.init(Builtin.ptrtoint_Word(objectID._value))
    }
  }

  extension Int {
    /// Create an `Int` that captures the full value of `objectID`.
    public init(bitPattern objectID: ObjectIdentifier) {
      self.init(bitPattern: UInt(objectID))
    }
  }

Motivation

···

==========

Adding a label ‘bitPattern’ to the constructors makes it clear that we interpret the pointer value as a bit pattern at the use site. It is similar to what we do in other APIs, for example in "UInt(bitPattern: UnsafePointer<Void>(value)))"

Impact on existing code

Existing code will have to add the bitPattern label.

Alternatives

Leave as is.


(Arnold Schwaighofer) #2

This is probably uncontroversial.

PR: https://github.com/apple/swift-evolution/pull/434

···

On Jul 13, 2016, at 4:39 PM, Arnold Schwaighofer via swift-evolution <swift-evolution@swift.org> wrote:

Int.init(ObjectIdentifier) and UInt.init(ObjectIdentifier) should have a 'bitPattern:’ label to make it clear at the use site that we interpret the value as a bit pattern.

In Swift we have ObjectIdentifier values which uniquely identify a class instance or metatype. They are implemented as a struct which holds the value of the reference to the instance or metatype as a raw pointer.

/// A unique identifier for a class instance or metatype.
public struct ObjectIdentifier : Hashable, Comparable {
   internal let _value: Builtin.RawPointer
   ...
}

We have constructors for Int and Uint that capture this value. These constructors don’t have an argument label.

extension UInt {
   /// Create a `UInt` that captures the full value of `objectID`.
   public init(_ objectID: ObjectIdentifier) {
     self.init(Builtin.ptrtoint_Word(objectID._value))
   }
}

extension Int {
   /// Create an `Int` that captures the full value of `objectID`.
   public init(_ objectID: ObjectIdentifier) {
     self.init(bitPattern: UInt(objectID))
   }
}

This proposals suggest adding a label ‘bitPattern’ to the constructor: Int.init(bitPattern: ObjectIdentifier).

extension UInt {
   /// Create a `UInt` that captures the full value of `objectID`.
   public init(bitPattern objectID: ObjectIdentifier) {
     self.init(Builtin.ptrtoint_Word(objectID._value))
   }
}

extension Int {
   /// Create an `Int` that captures the full value of `objectID`.
   public init(bitPattern objectID: ObjectIdentifier) {
     self.init(bitPattern: UInt(objectID))
   }
}

Motivation

Adding a label ‘bitPattern’ to the constructors makes it clear that we interpret the pointer value as a bit pattern at the use site. It is similar to what we do in other APIs, for example in "UInt(bitPattern: UnsafePointer<Void>(value)))"

Impact on existing code

Existing code will have to add the bitPattern label.

Alternatives

Leave as is.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Dmitri Gribenko) #3

+1!

Dmitri

···

On Wed, Jul 13, 2016 at 4:39 PM, Arnold Schwaighofer via swift-evolution <swift-evolution@swift.org> wrote:

Int.init(ObjectIdentifier) and UInt.init(ObjectIdentifier) should have a 'bitPattern:’ label to make it clear at the use site that we interpret the value as a bit pattern.

--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr@gmail.com>*/


(David Sweeris) #4

+1

···

Sent from my iPhone

On Jul 13, 2016, at 18:39, Arnold Schwaighofer via swift-evolution <swift-evolution@swift.org> wrote:

Int.init(ObjectIdentifier) and UInt.init(ObjectIdentifier) should have a 'bitPattern:’ label to make it clear at the use site that we interpret the value as a bit pattern.

In Swift we have ObjectIdentifier values which uniquely identify a class instance or metatype. They are implemented as a struct which holds the value of the reference to the instance or metatype as a raw pointer.

/// A unique identifier for a class instance or metatype.
public struct ObjectIdentifier : Hashable, Comparable {
   internal let _value: Builtin.RawPointer
   ...
}

We have constructors for Int and Uint that capture this value. These constructors don’t have an argument label.

extension UInt {
   /// Create a `UInt` that captures the full value of `objectID`.
   public init(_ objectID: ObjectIdentifier) {
     self.init(Builtin.ptrtoint_Word(objectID._value))
   }
}

extension Int {
   /// Create an `Int` that captures the full value of `objectID`.
   public init(_ objectID: ObjectIdentifier) {
     self.init(bitPattern: UInt(objectID))
   }
}

This proposals suggest adding a label ‘bitPattern’ to the constructor: Int.init(bitPattern: ObjectIdentifier).

extension UInt {
   /// Create a `UInt` that captures the full value of `objectID`.
   public init(bitPattern objectID: ObjectIdentifier) {
     self.init(Builtin.ptrtoint_Word(objectID._value))
   }
}

extension Int {
   /// Create an `Int` that captures the full value of `objectID`.
   public init(bitPattern objectID: ObjectIdentifier) {
     self.init(bitPattern: UInt(objectID))
   }
}

Motivation

Adding a label ‘bitPattern’ to the constructors makes it clear that we interpret the pointer value as a bit pattern at the use site. It is similar to what we do in other APIs, for example in "UInt(bitPattern: UnsafePointer<Void>(value)))"

Impact on existing code

Existing code will have to add the bitPattern label.

Alternatives

Leave as is.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Dave Abrahams) #5

This is probably uncontroversial.

PR: https://github.com/apple/swift-evolution/pull/434

+1

···

on Fri Jul 15 2016, Arnold Schwaighofer <swift-evolution@swift.org> wrote:

On Jul 13, 2016, at 4:39 PM, Arnold Schwaighofer via swift-evolution <swift-evolution@swift.org> wrote:

Int.init(ObjectIdentifier) and UInt.init(ObjectIdentifier) should have a 'bitPattern:’ label to make it clear at the use site that we interpret the value as a bit pattern.

In Swift we have ObjectIdentifier values which uniquely identify a class instance or metatype. They are implemented as a struct which holds the value of the reference to the instance or metatype as a raw pointer.

/// A unique identifier for a class instance or metatype.
public struct ObjectIdentifier : Hashable, Comparable {
   internal let _value: Builtin.RawPointer
   ...
}

We have constructors for Int and Uint that capture this value. These constructors don’t have an argument label.

extension UInt {
   /// Create a `UInt` that captures the full value of `objectID`.
   public init(_ objectID: ObjectIdentifier) {
     self.init(Builtin.ptrtoint_Word(objectID._value))
   }
}

extension Int {
   /// Create an `Int` that captures the full value of `objectID`.
   public init(_ objectID: ObjectIdentifier) {
     self.init(bitPattern: UInt(objectID))
   }
}

This proposals suggest adding a label ‘bitPattern’ to the constructor: Int.init(bitPattern: ObjectIdentifier).

extension UInt {
   /// Create a `UInt` that captures the full value of `objectID`.
   public init(bitPattern objectID: ObjectIdentifier) {
     self.init(Builtin.ptrtoint_Word(objectID._value))
   }
}

extension Int {
   /// Create an `Int` that captures the full value of `objectID`.
   public init(bitPattern objectID: ObjectIdentifier) {
     self.init(bitPattern: UInt(objectID))
   }
}

Motivation

Adding a label ‘bitPattern’ to the constructors makes it clear that we interpret the pointer value as a bit pattern at the use site. It is similar to what we do in other APIs, for example in "UInt(bitPattern: UnsafePointer<Void>(value)))"

Impact on existing code

Existing code will have to add the bitPattern label.

Alternatives

Leave as is.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
Dave


(Charlie Monroe) #6

+1. Agreed. Otherwise, it's kind of a cryptic initializer.

···

On Jul 15, 2016, at 3:54 PM, Arnold Schwaighofer via swift-evolution <swift-evolution@swift.org> wrote:

This is probably uncontroversial.

PR: https://github.com/apple/swift-evolution/pull/434

On Jul 13, 2016, at 4:39 PM, Arnold Schwaighofer via swift-evolution <swift-evolution@swift.org> wrote:

Int.init(ObjectIdentifier) and UInt.init(ObjectIdentifier) should have a 'bitPattern:’ label to make it clear at the use site that we interpret the value as a bit pattern.

In Swift we have ObjectIdentifier values which uniquely identify a class instance or metatype. They are implemented as a struct which holds the value of the reference to the instance or metatype as a raw pointer.

/// A unique identifier for a class instance or metatype.
public struct ObjectIdentifier : Hashable, Comparable {
  internal let _value: Builtin.RawPointer
  ...
}

We have constructors for Int and Uint that capture this value. These constructors don’t have an argument label.

extension UInt {
  /// Create a `UInt` that captures the full value of `objectID`.
  public init(_ objectID: ObjectIdentifier) {
    self.init(Builtin.ptrtoint_Word(objectID._value))
  }
}

extension Int {
  /// Create an `Int` that captures the full value of `objectID`.
  public init(_ objectID: ObjectIdentifier) {
    self.init(bitPattern: UInt(objectID))
  }
}

This proposals suggest adding a label ‘bitPattern’ to the constructor: Int.init(bitPattern: ObjectIdentifier).

extension UInt {
  /// Create a `UInt` that captures the full value of `objectID`.
  public init(bitPattern objectID: ObjectIdentifier) {
    self.init(Builtin.ptrtoint_Word(objectID._value))
  }
}

extension Int {
  /// Create an `Int` that captures the full value of `objectID`.
  public init(bitPattern objectID: ObjectIdentifier) {
    self.init(bitPattern: UInt(objectID))
  }
}

Motivation

Adding a label ‘bitPattern’ to the constructors makes it clear that we interpret the pointer value as a bit pattern at the use site. It is similar to what we do in other APIs, for example in "UInt(bitPattern: UnsafePointer<Void>(value)))"

Impact on existing code

Existing code will have to add the bitPattern label.

Alternatives

Leave as is.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution