Bringing Back Some Old Friends

The designers of Swift seem to have really odd opinions regarding the merits of certain patterns that are very common across other languages. For example, they have deemed that the prefix and postfix operators “++” and “–” are, for some reason, not desirable. Weird. 🧐

Not a biggie because they also built Swift with the ability for us to easily bring them back. Below are a few lines of code that you can add to your own projects to bring “++” and “–” back for integer based variables.

The generics pattern “<T: BinaryInteger>” allows us to use these new operators with any integer variable such as Int, UInt, Int8, Int32, etc.

/*===============================================================================================================================*/
/// Let's bring back some important operators!
///
prefix operator ++
prefix operator --
postfix operator ++
postfix operator --

/*===============================================================================================================================*/
/// Implements the `++` prefix operator as found in C, C++, Objective-C, Java, and many other languages.
/// 
/// - Parameter operand: the integer variable.
/// - Returns: the value of the `operand` AFTER incrementing it's value by 1.
///
@inlinable public prefix func ++ <T: BinaryInteger>(operand: inout T) -> T {
    operand += 1
    return operand
}

/*===============================================================================================================================*/
/// Implements the `--` prefix operator as found in C, C++, Objective-C, Java, and many other languages.
/// 
/// - Parameter operand: the integer variable.
/// - Returns: the value of the `operand` AFTER deccrementing it's value by 1.
///
@inlinable public prefix func -- <T: BinaryInteger>(operand: inout T) -> T {
    operand -= 1
    return operand
}

/*===============================================================================================================================*/
/// Implements the `++` postfix operator as found in C, C++, Objective-C, Java, and many other languages.
/// 
/// - Parameter operand: the integer variable.
/// - Returns: the value of the `operand` BEFORE incrementing it's value by 1.
///
@inlinable public postfix func ++ <T: BinaryInteger>(operand: inout T) -> T {
    let i: T = operand
    operand += 1
    return i
}

/*===============================================================================================================================*/
/// Implements the `--` postfix operator as found in C, C++, Objective-C, Java, and many other languages.
/// 
/// - Parameter operand: the integer variable.
/// - Returns: the value of the `operand` BEFORE deccrementing it's value by 1.
///
@inlinable public postfix func -- <T: BinaryInteger>(operand: inout T) -> T {
    let i: T = operand
    operand -= 1
    return i
}

With these new operators we can do the following:

var intVariable: Int = 1
print("The new value is \(++intVariable)")
	

This will output the line:

The new value is 2

Leave a Reply