Skip to content

Instantly share code, notes, and snippets.

@phranck
Last active February 27, 2024 20:57
Show Gist options
  • Save phranck/ee629a99e71899f59abcbe3c8848516a to your computer and use it in GitHub Desktop.
Save phranck/ee629a99e71899f59abcbe3c8848516a to your computer and use it in GitHub Desktop.
/// The `=!=` operator assigns the value on the right-hand side to the variable on the left-hand side
/// only if the two values are not equal. This operator is designed for types conforming to the `Equatable` protocol.
///
/// - Parameters:
/// - lhs: The variable to be assigned a new value if not equal.
/// - rhs: The value to be assigned to the variable if it is not equal to the current value.
///
/// - Precondition: The type of the operands must conform to the `Equatable` protocol.
///
/// - Precedence: `AssignIfNotEqualPrecedence`
///
/// - Complexity: O(1)
///
/// - Note: The assignment operation is performed only if `lhs != rhs`.
///
/// - Example:
/// ```swift
/// var x = 42
/// let y = 24
/// x =!= y // x is now 24
/// ```
///
infix operator =!=: AssignIfNotEqualPrecedence
public func =!=<T>(lhs: inout T, rhs: T) where T: Equatable {
if lhs != rhs { lhs = rhs }
}
precedencegroup AssignIfNotEqualPrecedence {
lowerThan: ComparisonPrecedence
higherThan: AssignmentPrecedence
associativity: left
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment