Skip to content

Commit d6dec00

Browse files
natecook1000moiseev
authored andcommitted
[Integer protocols] Make .negate() the customization point (#4413)
The SignedArithmetic protocol provides default implementations for both negate() and negated(), with negate() calling negated(). This is inconsistent with the Arithmetic protocol, which provides implementations of the nonmutating methods that call their mutating counterparts. This flips the implementations of negate() and negated() so an implementer only has to focus on mutating methods.
1 parent 4af655e commit d6dec00

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

stdlib/public/core/Integers.swift.gyb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,14 @@ public protocol SignedArithmetic : Arithmetic {
159159
extension SignedArithmetic {
160160
@_transparent
161161
public func negated() -> Self {
162-
return Self().subtracting(self)
162+
var result = self
163+
result.negate()
164+
return result
163165
}
164166

165167
@_transparent
166168
public mutating func negate() {
167-
self = negated()
169+
self = Self().subtracting(self)
168170
}
169171
}
170172

0 commit comments

Comments
 (0)