Skip to content

Commit 94dc4a6

Browse files
authored
Merge pull request swiftlang#20422 from rxwei/additive-arithmetic
2 parents d24f9d2 + aeadc14 commit 94dc4a6

File tree

6 files changed

+126
-66
lines changed

6 files changed

+126
-66
lines changed

stdlib/public/core/Integers.swift

Lines changed: 81 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,89 @@ extension ExpressibleByIntegerLiteral
2323
}
2424
}
2525

26+
//===----------------------------------------------------------------------===//
27+
//===--- AdditiveArithmetic -----------------------------------------------===//
28+
//===----------------------------------------------------------------------===//
29+
30+
// FIXME: Add doc comment.
31+
public protocol AdditiveArithmetic : Equatable {
32+
/// The zero value.
33+
///
34+
/// - Note: Zero is the identity element for addition; for any value,
35+
/// `x + .zero == x` and `.zero + x == x`.
36+
static var zero: Self { get }
37+
38+
/// Adds two values and produces their sum.
39+
///
40+
/// The addition operator (`+`) calculates the sum of its two arguments. For
41+
/// example:
42+
///
43+
/// 1 + 2 // 3
44+
/// -10 + 15 // 5
45+
/// -15 + -5 // -20
46+
/// 21.5 + 3.25 // 24.75
47+
///
48+
/// You cannot use `+` with arguments of different types. To add values of
49+
/// different types, convert one of the values to the other value's type.
50+
///
51+
/// let x: Int8 = 21
52+
/// let y: Int = 1000000
53+
/// Int(x) + y // 1000021
54+
///
55+
/// - Parameters:
56+
/// - lhs: The first value to add.
57+
/// - rhs: The second value to add.
58+
static func +(lhs: Self, rhs: Self) -> Self
59+
60+
/// Adds two values and stores the result in the left-hand-side variable.
61+
///
62+
/// - Parameters:
63+
/// - lhs: The first value to add.
64+
/// - rhs: The second value to add.
65+
static func +=(lhs: inout Self, rhs: Self)
66+
67+
/// Subtracts one value from another and produces their difference.
68+
///
69+
/// The subtraction operator (`-`) calculates the difference of its two
70+
/// arguments. For example:
71+
///
72+
/// 8 - 3 // 5
73+
/// -10 - 5 // -15
74+
/// 100 - -5 // 105
75+
/// 10.5 - 100.0 // -89.5
76+
///
77+
/// You cannot use `-` with arguments of different types. To subtract values
78+
/// of different types, convert one of the values to the other value's type.
79+
///
80+
/// let x: UInt8 = 21
81+
/// let y: UInt = 1000000
82+
/// y - UInt(x) // 999979
83+
///
84+
/// - Parameters:
85+
/// - lhs: A numeric value.
86+
/// - rhs: The value to subtract from `lhs`.
87+
static func -(lhs: Self, rhs: Self) -> Self
88+
89+
/// Subtracts the second value from the first and stores the difference in the
90+
/// left-hand-side variable.
91+
///
92+
/// - Parameters:
93+
/// - lhs: A numeric value.
94+
/// - rhs: The value to subtract from `lhs`.
95+
static func -=(lhs: inout Self, rhs: Self)
96+
}
97+
98+
public extension AdditiveArithmetic where Self : ExpressibleByIntegerLiteral {
99+
static var zero: Self {
100+
return 0
101+
}
102+
}
103+
26104
//===----------------------------------------------------------------------===//
27105
//===--- Numeric ----------------------------------------------------------===//
28106
//===----------------------------------------------------------------------===//
29107

108+
// FIXME: Update comment based on the `AdditiveArithmetic` change.
30109
/// Declares methods backing binary arithmetic operators--such as `+`, `-` and
31110
/// `*`--and their mutating counterparts.
32111
///
@@ -61,7 +140,7 @@ extension ExpressibleByIntegerLiteral
61140
/// the required mutating methods. Extensions to `Numeric` provide default
62141
/// implementations for the protocol's nonmutating methods based on the
63142
/// mutating variants.
64-
public protocol Numeric : Equatable, ExpressibleByIntegerLiteral {
143+
public protocol Numeric : AdditiveArithmetic, ExpressibleByIntegerLiteral {
65144
/// Creates a new instance from the given integer, if it can be represented
66145
/// exactly.
67146
///
@@ -100,65 +179,6 @@ public protocol Numeric : Equatable, ExpressibleByIntegerLiteral {
100179
/// instead of the `magnitude` property is encouraged.
101180
var magnitude: Magnitude { get }
102181

103-
/// Adds two values and produces their sum.
104-
///
105-
/// The addition operator (`+`) calculates the sum of its two arguments. For
106-
/// example:
107-
///
108-
/// 1 + 2 // 3
109-
/// -10 + 15 // 5
110-
/// -15 + -5 // -20
111-
/// 21.5 + 3.25 // 24.75
112-
///
113-
/// You cannot use `+` with arguments of different types. To add values of
114-
/// different types, convert one of the values to the other value's type.
115-
///
116-
/// let x: Int8 = 21
117-
/// let y: Int = 1000000
118-
/// Int(x) + y // 1000021
119-
///
120-
/// - Parameters:
121-
/// - lhs: The first value to add.
122-
/// - rhs: The second value to add.
123-
static func +(lhs: Self, rhs: Self) -> Self
124-
125-
/// Adds two values and stores the result in the left-hand-side variable.
126-
///
127-
/// - Parameters:
128-
/// - lhs: The first value to add.
129-
/// - rhs: The second value to add.
130-
static func +=(lhs: inout Self, rhs: Self)
131-
132-
/// Subtracts one value from another and produces their difference.
133-
///
134-
/// The subtraction operator (`-`) calculates the difference of its two
135-
/// arguments. For example:
136-
///
137-
/// 8 - 3 // 5
138-
/// -10 - 5 // -15
139-
/// 100 - -5 // 105
140-
/// 10.5 - 100.0 // -89.5
141-
///
142-
/// You cannot use `-` with arguments of different types. To subtract values
143-
/// of different types, convert one of the values to the other value's type.
144-
///
145-
/// let x: UInt8 = 21
146-
/// let y: UInt = 1000000
147-
/// y - UInt(x) // 999979
148-
///
149-
/// - Parameters:
150-
/// - lhs: A numeric value.
151-
/// - rhs: The value to subtract from `lhs`.
152-
static func -(lhs: Self, rhs: Self) -> Self
153-
154-
/// Subtracts the second value from the first and stores the difference in the
155-
/// left-hand-side variable.
156-
///
157-
/// - Parameters:
158-
/// - lhs: A numeric value.
159-
/// - rhs: The value to subtract from `lhs`.
160-
static func -=(lhs: inout Self, rhs: Self)
161-
162182
/// Multiplies two values and produces their product.
163183
///
164184
/// The multiplication operator (`*`) calculates the product of its two
@@ -327,7 +347,7 @@ public func abs<T : SignedNumeric & Comparable>(_ x: T) -> T {
327347
return x < (0 as T) ? -x : x
328348
}
329349

330-
extension Numeric {
350+
extension AdditiveArithmetic {
331351
/// Returns the given number unchanged.
332352
///
333353
/// You can use the unary plus operator (`+`) to provide symmetry in your

stdlib/public/core/Policy.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ prefix operator ++
396396
prefix operator --
397397
prefix operator !
398398
prefix operator ~ : BinaryInteger
399-
prefix operator + : Numeric
399+
prefix operator + : AdditiveArithmetic
400400
prefix operator - : SignedNumeric
401401
prefix operator ...
402402
prefix operator ..<
@@ -420,9 +420,9 @@ infix operator & : MultiplicationPrecedence, BinaryInteger
420420

421421
// "Additive"
422422

423-
infix operator + : AdditionPrecedence, Numeric, String, Strideable
423+
infix operator + : AdditionPrecedence, AdditiveArithmetic, String, Strideable
424424
infix operator &+ : AdditionPrecedence, FixedWidthInteger
425-
infix operator - : AdditionPrecedence, Numeric, Strideable
425+
infix operator - : AdditionPrecedence, AdditiveArithmetic, Strideable
426426
infix operator &- : AdditionPrecedence, FixedWidthInteger
427427
infix operator | : AdditionPrecedence, BinaryInteger
428428
infix operator ^ : AdditionPrecedence, BinaryInteger
@@ -474,9 +474,9 @@ infix operator *= : AssignmentPrecedence, Numeric
474474
infix operator &*= : AssignmentPrecedence, FixedWidthInteger
475475
infix operator /= : AssignmentPrecedence, BinaryInteger
476476
infix operator %= : AssignmentPrecedence, BinaryInteger
477-
infix operator += : AssignmentPrecedence, Numeric, String, Strideable
477+
infix operator += : AssignmentPrecedence, AdditiveArithmetic, String, Strideable
478478
infix operator &+= : AssignmentPrecedence, FixedWidthInteger
479-
infix operator -= : AssignmentPrecedence, Numeric, Strideable
479+
infix operator -= : AssignmentPrecedence, AdditiveArithmetic, Strideable
480480
infix operator &-= : AssignmentPrecedence, FixedWidthInteger
481481
infix operator <<= : AssignmentPrecedence, BinaryInteger
482482
infix operator &<<= : AssignmentPrecedence, FixedWidthInteger

test/api-digester/Outputs/cake-abi.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,11 @@
13421342
}
13431343
]
13441344
},
1345+
{
1346+
"kind": "Conformance",
1347+
"name": "AdditiveArithmetic",
1348+
"printedName": "AdditiveArithmetic"
1349+
},
13451350
{
13461351
"kind": "Conformance",
13471352
"name": "ExpressibleByIntegerLiteral",

test/api-digester/Outputs/cake.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,11 @@
12361236
}
12371237
]
12381238
},
1239+
{
1240+
"kind": "Conformance",
1241+
"name": "AdditiveArithmetic",
1242+
"printedName": "AdditiveArithmetic"
1243+
},
12391244
{
12401245
"kind": "Conformance",
12411246
"name": "ExpressibleByIntegerLiteral",

test/api-digester/Outputs/stability-stdlib-abi.swift.expected

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,19 @@ Func SystemRandomNumberGenerator._fill(bytes:) has been removed
154154

155155
Func _createStringTableCache(_:) has been removed
156156
Struct _StringSwitchContext has been removed
157+
158+
Protocol Numeric has generic signature change from <τ_0_0 : Equatable, τ_0_0 : ExpressibleByIntegerLiteral, τ_0_0.Magnitude : Comparable, τ_0_0.Magnitude : Numeric> to <τ_0_0 : AdditiveArithmetic, τ_0_0 : ExpressibleByIntegerLiteral, τ_0_0.Magnitude : Comparable, τ_0_0.Magnitude : Numeric>
159+
Func Numeric.+(_:) has been removed
160+
Func Numeric.+(_:_:) has been removed
161+
Func Numeric.+=(_:_:) has been removed
162+
Func Numeric.-(_:_:) has been removed
163+
Func Numeric.-=(_:_:) has been removed
164+
Protocol BinaryFloatingPoint has added inherited protocol AdditiveArithmetic
165+
Protocol BinaryInteger has added inherited protocol AdditiveArithmetic
166+
Protocol FixedWidthInteger has added inherited protocol AdditiveArithmetic
167+
Protocol FloatingPoint has added inherited protocol AdditiveArithmetic
168+
Protocol Numeric has added inherited protocol AdditiveArithmetic
169+
Protocol SignedInteger has added inherited protocol AdditiveArithmetic
170+
Protocol SignedNumeric has added inherited protocol AdditiveArithmetic
171+
Protocol UnsignedInteger has added inherited protocol AdditiveArithmetic
172+

test/api-digester/Outputs/stability-stdlib-source.swift.expected

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
/* Generic Signature Changes */
3+
Protocol Numeric has generic signature change from <Self : Equatable, Self : ExpressibleByIntegerLiteral, Self.Magnitude : Comparable, Self.Magnitude : Numeric> to <Self : AdditiveArithmetic, Self : ExpressibleByIntegerLiteral, Self.Magnitude : Comparable, Self.Magnitude : Numeric>
34
Protocol StringProtocol has generic signature change from <Self : BidirectionalCollection, Self : Comparable, Self : ExpressibleByStringLiteral, Self : Hashable, Self : LosslessStringConvertible, Self : TextOutputStream, Self : TextOutputStreamable, Self.Element == Character, Self.Index == String.Index, Self.SubSequence : StringProtocol, Self.UTF16View : BidirectionalCollection, Self.UTF8View : Collection, Self.UnicodeScalarView : BidirectionalCollection, Self.UTF16View.Element == UInt16, Self.UTF16View.Index == String.Index, Self.UTF8View.Element == UInt8, Self.UTF8View.Index == String.Index, Self.UnicodeScalarView.Element == Unicode.Scalar, Self.UnicodeScalarView.Index == String.Index, Self.SubSequence.UTF16View.Index == String.Index, Self.SubSequence.UTF8View.Index == String.Index, Self.SubSequence.UnicodeScalarView.Index == String.Index> to <Self : BidirectionalCollection, Self : Comparable, Self : ExpressibleByStringInterpolation, Self : Hashable, Self : LosslessStringConvertible, Self : TextOutputStream, Self : TextOutputStreamable, Self.Element == Character, Self.Index == String.Index, Self.StringInterpolation == DefaultStringInterpolation, Self.SubSequence : StringProtocol, Self.UTF16View : BidirectionalCollection, Self.UTF8View : Collection, Self.UnicodeScalarView : BidirectionalCollection, Self.UTF16View.Element == UInt16, Self.UTF16View.Index == String.Index, Self.UTF8View.Element == UInt8, Self.UTF8View.Index == String.Index, Self.UnicodeScalarView.Element == Unicode.Scalar, Self.UnicodeScalarView.Index == String.Index, Self.SubSequence.UTF16View.Index == String.Index, Self.SubSequence.UTF8View.Index == String.Index, Self.SubSequence.UnicodeScalarView.Index == String.Index>
45

56
/* RawRepresentable Changes */
@@ -9,6 +10,11 @@ Constructor String.init(stringInterpolationSegment:) has been removed
910
Func Collection.prefix(through:) has been removed
1011
Func Collection.prefix(upTo:) has been removed
1112
Func Collection.suffix(from:) has been removed
13+
Func Numeric.+(_:) has been removed
14+
Func Numeric.+(_:_:) has been removed
15+
Func Numeric.+=(_:_:) has been removed
16+
Func Numeric.-(_:_:) has been removed
17+
Func Numeric.-=(_:_:) has been removed
1218
Func Sequence.filter(_:) has been removed
1319
Func Sequence.forEach(_:) has been removed
1420
Func Sequence.map(_:) has been removed
@@ -36,8 +42,16 @@ Var Set.first has been removed
3642

3743
/* Type Changes */
3844
Constructor String.init(stringInterpolation:) has parameter 0 type change from [String] to DefaultStringInterpolation
45+
Protocol BinaryFloatingPoint has added inherited protocol AdditiveArithmetic
46+
Protocol BinaryInteger has added inherited protocol AdditiveArithmetic
47+
Protocol FixedWidthInteger has added inherited protocol AdditiveArithmetic
48+
Protocol FloatingPoint has added inherited protocol AdditiveArithmetic
49+
Protocol Numeric has added inherited protocol AdditiveArithmetic
50+
Protocol SignedInteger has added inherited protocol AdditiveArithmetic
51+
Protocol SignedNumeric has added inherited protocol AdditiveArithmetic
3952

4053
/* Decl Attribute changes */
4154

4255
/* Protocol Requirement Changes */
4356
Protocol StringProtocol has added inherited protocol ExpressibleByStringInterpolation
57+
Protocol UnsignedInteger has added inherited protocol AdditiveArithmetic

0 commit comments

Comments
 (0)