swift 3> swift4 conversion and overflow operators.

All,

The documentation for swift4 shows the following are still valid


  • Overflow addition (
    &+
    )
  • Overflow subtraction (
    &-
    )
  • Overflow multiplication (
    &*
    )

when I convert my swift 3 code

public extension Integer
{
  var lowbit: Self { return self & (~self &+ 1) }
  var isPower2 : Bool { return 0 < self && self & (self &- 1) == 0 }
}


I get the following errors

Binary operator '&+' cannot be applied to operands of type 'Self' and 'Int'


Did I miss something in the release notes?


Thanks for the help.

Accepted Reply

>> Did I miss something in the release notes?


Actually, yes, you did. The release notes (https://developer.apple.com/library/content/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html) say this:


A new family of integer protocols enables more generic algorithms:

• Numeric, types that support arithmetic operators

• BinaryInteger, integer types that have a binary representation and support bitwise operations

• FixedWidthInteger, integer types that use a fixed size and support the concept of arithmetic operations that report overflow


As Claude pointed out, the old "Integer" protocol has been renamed, but the overflow-reporting operators were moved to a new protocol, FixedWidthInteger (because overflow only fully makes sense for integers with a fixed width representation). That means the correct "fix" is along these lines:


public extension FixedWidthInteger
{
  var lowbit: Self { return self & (~self &+ 1) }
}


You can read up on all the gory details in SE-104 (github.com/apple/swift-evolution/blob/master/proposals/0104-improved-integers.md).

Replies

When I try in XCode 9.2 (Swift4), I get 2 errors :


Integer has been renamed BinaryInteger

And the same error as yours


If I replace the definition of extension by

public extension UInt32
{
  var lowbit: UInt32 { return self & (~self &+ 1) }
}

no more error


Seems that &+ works with unsigned only

>> Did I miss something in the release notes?


Actually, yes, you did. The release notes (https://developer.apple.com/library/content/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html) say this:


A new family of integer protocols enables more generic algorithms:

• Numeric, types that support arithmetic operators

• BinaryInteger, integer types that have a binary representation and support bitwise operations

• FixedWidthInteger, integer types that use a fixed size and support the concept of arithmetic operations that report overflow


As Claude pointed out, the old "Integer" protocol has been renamed, but the overflow-reporting operators were moved to a new protocol, FixedWidthInteger (because overflow only fully makes sense for integers with a fixed width representation). That means the correct "fix" is along these lines:


public extension FixedWidthInteger
{
  var lowbit: Self { return self & (~self &+ 1) }
}


You can read up on all the gory details in SE-104 (github.com/apple/swift-evolution/blob/master/proposals/0104-improved-integers.md).

Great thank you.