@@ -23,10 +23,89 @@ extension ExpressibleByIntegerLiteral
23
23
}
24
24
}
25
25
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
+
26
104
//===----------------------------------------------------------------------===//
27
105
//===--- Numeric ----------------------------------------------------------===//
28
106
//===----------------------------------------------------------------------===//
29
107
108
+ // FIXME: Update comment based on the `AdditiveArithmetic` change.
30
109
/// Declares methods backing binary arithmetic operators--such as `+`, `-` and
31
110
/// `*`--and their mutating counterparts.
32
111
///
@@ -61,7 +140,7 @@ extension ExpressibleByIntegerLiteral
61
140
/// the required mutating methods. Extensions to `Numeric` provide default
62
141
/// implementations for the protocol's nonmutating methods based on the
63
142
/// mutating variants.
64
- public protocol Numeric : Equatable , ExpressibleByIntegerLiteral {
143
+ public protocol Numeric : AdditiveArithmetic , ExpressibleByIntegerLiteral {
65
144
/// Creates a new instance from the given integer, if it can be represented
66
145
/// exactly.
67
146
///
@@ -100,65 +179,6 @@ public protocol Numeric : Equatable, ExpressibleByIntegerLiteral {
100
179
/// instead of the `magnitude` property is encouraged.
101
180
var magnitude : Magnitude { get }
102
181
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
-
162
182
/// Multiplies two values and produces their product.
163
183
///
164
184
/// The multiplication operator (`*`) calculates the product of its two
@@ -327,7 +347,7 @@ public func abs<T : SignedNumeric & Comparable>(_ x: T) -> T {
327
347
return x < ( 0 as T ) ? - x : x
328
348
}
329
349
330
- extension Numeric {
350
+ extension AdditiveArithmetic {
331
351
/// Returns the given number unchanged.
332
352
///
333
353
/// You can use the unary plus operator (`+`) to provide symmetry in your
0 commit comments