Skip to content

Commit 504cad7

Browse files
committed
[Measurement] Group operators into just two extensions
1 parent 67a5d39 commit 504cad7

File tree

1 file changed

+80
-109
lines changed

1 file changed

+80
-109
lines changed

Foundation/Measurement.swift

Lines changed: 80 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,7 @@ extension Measurement where UnitType : Dimension {
6969
public mutating func convert(to otherUnit: UnitType) {
7070
self = converted(to: otherUnit)
7171
}
72-
73-
}
74-
75-
extension Measurement {
76-
/// Add two measurements of the same Unit.
77-
/// - precondition: The `unit` of `lhs` and `rhs` must be `isEqual`.
78-
/// - returns: A measurement of value `lhs.value + rhs.value` and unit `lhs.unit`.
79-
public static func +(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Measurement<UnitType> {
80-
if lhs.unit.isEqual(rhs.unit) {
81-
return Measurement(value: lhs.value + rhs.value, unit: lhs.unit)
82-
} else {
83-
fatalError("Attempt to add measurements with non-equal units")
84-
}
85-
}
86-
}
8772

88-
extension Measurement where UnitType : Dimension {
8973
/// Add two measurements of the same Dimension.
9074
///
9175
/// If the `unit` of the `lhs` and `rhs` are `isEqual`, then this returns the result of adding the `value` of each `Measurement`. If they are not equal, then this will convert both to the base unit of the `Dimension` and return the result as a `Measurement` of that base unit.
@@ -99,22 +83,7 @@ extension Measurement where UnitType : Dimension {
9983
return Measurement(value: lhsValueInTermsOfBase + rhsValueInTermsOfBase, unit: type(of: lhs.unit).baseUnit())
10084
}
10185
}
102-
}
10386

104-
extension Measurement {
105-
/// Subtract two measurements of the same Unit.
106-
/// - precondition: The `unit` of `lhs` and `rhs` must be `isEqual`.
107-
/// - returns: A measurement of value `lhs.value - rhs.value` and unit `lhs.unit`.
108-
public static func -(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Measurement<UnitType> {
109-
if lhs.unit.isEqual(rhs.unit) {
110-
return Measurement(value: lhs.value - rhs.value, unit: lhs.unit)
111-
} else {
112-
fatalError("Attempt to subtract measurements with non-equal units")
113-
}
114-
}
115-
}
116-
117-
extension Measurement where UnitType : Dimension {
11887
/// Subtract two measurements of the same Dimension.
11988
///
12089
/// If the `unit` of the `lhs` and `rhs` are `==`, then this returns the result of subtracting the `value` of each `Measurement`. If they are not equal, then this will convert both to the base unit of the `Dimension` and return the result as a `Measurement` of that base unit.
@@ -128,41 +97,7 @@ extension Measurement where UnitType : Dimension {
12897
return Measurement(value: lhsValueInTermsOfBase - rhsValueInTermsOfBase, unit: type(of: lhs.unit).baseUnit())
12998
}
13099
}
131-
}
132-
133-
extension Measurement {
134-
/// Multiply a measurement by a scalar value.
135-
/// - returns: A measurement of value `lhs.value * rhs` with the same unit as `lhs`.
136-
public static func *(lhs: Measurement<UnitType>, rhs: Double) -> Measurement<UnitType> {
137-
return Measurement(value: lhs.value * rhs, unit: lhs.unit)
138-
}
139-
140-
/// Multiply a scalar value by a measurement.
141-
/// - returns: A measurement of value `lhs * rhs.value` with the same unit as `rhs`.
142-
public static func *(lhs: Double, rhs: Measurement<UnitType>) -> Measurement<UnitType> {
143-
return Measurement(value: lhs * rhs.value, unit: rhs.unit)
144-
}
145-
146-
/// Divide a measurement by a scalar value.
147-
/// - returns: A measurement of value `lhs.value / rhs` with the same unit as `lhs`.
148-
public static func /(lhs: Measurement<UnitType>, rhs: Double) -> Measurement<UnitType> {
149-
return Measurement(value: lhs.value / rhs, unit: lhs.unit)
150-
}
151-
152-
/// Divide a scalar value by a measurement.
153-
/// - returns: A measurement of value `lhs / rhs.value` with the same unit as `rhs`.
154-
public static func /(lhs: Double, rhs: Measurement<UnitType>) -> Measurement<UnitType> {
155-
return Measurement(value: lhs / rhs.value, unit: rhs.unit)
156-
}
157-
158-
/// Compare two measurements of the same `Unit`.
159-
/// - returns: `true` if `lhs.value == rhs.value && lhs.unit == rhs.unit`.
160-
public static func ==(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Bool {
161-
return lhs.value == rhs.value && lhs.unit == rhs.unit
162-
}
163-
}
164100

165-
extension Measurement where UnitType : Dimension {
166101
/// Compare two measurements of the same `Dimension`.
167102
///
168103
/// If `lhs.unit == rhs.unit`, returns `lhs.value == rhs.value`. Otherwise, converts `rhs` to the same unit as `lhs` and then compares the resulting values.
@@ -175,18 +110,7 @@ extension Measurement where UnitType : Dimension {
175110
return lhs.value == rhsInLhs.value
176111
}
177112
}
178-
}
179113

180-
extension Measurement {
181-
/// Compare two measurements of the same `Unit`.
182-
/// - note: This function does not check `==` for the `unit` property of `lhs` and `rhs`.
183-
/// - returns: `lhs.value < rhs.value`
184-
public static func <(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Bool {
185-
return lhs.value < rhs.value
186-
}
187-
}
188-
189-
extension Measurement where UnitType : Dimension {
190114
/// Compare two measurements of the same `Dimension`.
191115
///
192116
/// If `lhs.unit == rhs.unit`, returns `lhs.value < rhs.value`. Otherwise, converts `rhs` to the same unit as `lhs` and then compares the resulting values.
@@ -199,18 +123,7 @@ extension Measurement where UnitType : Dimension {
199123
return lhs.value < rhsInLhs.value
200124
}
201125
}
202-
}
203126

204-
extension Measurement {
205-
/// Compare two measurements of the same `Unit`.
206-
/// - note: This function does not check `==` for the `unit` property of `lhs` and `rhs`.
207-
/// - returns: `lhs.value > rhs.value`
208-
public static func >(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Bool {
209-
return lhs.value > rhs.value
210-
}
211-
}
212-
213-
extension Measurement where UnitType : Dimension {
214127
/// Compare two measurements of the same `Dimension`.
215128
///
216129
/// If `lhs.unit == rhs.unit`, returns `lhs.value > rhs.value`. Otherwise, converts `rhs` to the same unit as `lhs` and then compares the resulting values.
@@ -223,18 +136,7 @@ extension Measurement where UnitType : Dimension {
223136
return lhs.value > rhsInLhs.value
224137
}
225138
}
226-
}
227139

228-
extension Measurement {
229-
/// Compare two measurements of the same `Unit`.
230-
/// - note: This function does not check `==` for the `unit` property of `lhs` and `rhs`.
231-
/// - returns: `lhs.value <= rhs.value`
232-
public static func <=(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Bool {
233-
return lhs.value <= rhs.value
234-
}
235-
}
236-
237-
extension Measurement where UnitType : Dimension {
238140
/// Compare two measurements of the same `Dimension`.
239141
///
240142
/// If `lhs.unit == rhs.unit`, returns `lhs.value < rhs.value`. Otherwise, converts `rhs` to the same unit as `lhs` and then compares the resulting values.
@@ -247,18 +149,7 @@ extension Measurement where UnitType : Dimension {
247149
return lhs.value <= rhsInLhs.value
248150
}
249151
}
250-
}
251-
252-
extension Measurement {
253-
/// Compare two measurements of the same `Unit`.
254-
/// - note: This function does not check `==` for the `unit` property of `lhs` and `rhs`.
255-
/// - returns: `lhs.value >= rhs.value`
256-
public static func >=(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Bool {
257-
return lhs.value >= rhs.value
258-
}
259-
}
260152

261-
extension Measurement where UnitType : Dimension {
262153
/// Compare two measurements of the same `Dimension`.
263154
///
264155
/// If `lhs.unit == rhs.unit`, returns `lhs.value >= rhs.value`. Otherwise, converts `rhs` to the same unit as `lhs` and then compares the resulting values.
@@ -273,6 +164,86 @@ extension Measurement where UnitType : Dimension {
273164
}
274165
}
275166

167+
extension Measurement {
168+
/// Add two measurements of the same Unit.
169+
/// - precondition: The `unit` of `lhs` and `rhs` must be `isEqual`.
170+
/// - returns: A measurement of value `lhs.value + rhs.value` and unit `lhs.unit`.
171+
public static func +(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Measurement<UnitType> {
172+
if lhs.unit.isEqual(rhs.unit) {
173+
return Measurement(value: lhs.value + rhs.value, unit: lhs.unit)
174+
} else {
175+
fatalError("Attempt to add measurements with non-equal units")
176+
}
177+
}
178+
179+
/// Subtract two measurements of the same Unit.
180+
/// - precondition: The `unit` of `lhs` and `rhs` must be `isEqual`.
181+
/// - returns: A measurement of value `lhs.value - rhs.value` and unit `lhs.unit`.
182+
public static func -(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Measurement<UnitType> {
183+
if lhs.unit.isEqual(rhs.unit) {
184+
return Measurement(value: lhs.value - rhs.value, unit: lhs.unit)
185+
} else {
186+
fatalError("Attempt to subtract measurements with non-equal units")
187+
}
188+
}
189+
190+
public static func *(lhs: Measurement<UnitType>, rhs: Double) -> Measurement<UnitType> {
191+
return Measurement(value: lhs.value * rhs, unit: lhs.unit)
192+
}
193+
194+
/// Multiply a scalar value by a measurement.
195+
/// - returns: A measurement of value `lhs * rhs.value` with the same unit as `rhs`.
196+
public static func *(lhs: Double, rhs: Measurement<UnitType>) -> Measurement<UnitType> {
197+
return Measurement(value: lhs * rhs.value, unit: rhs.unit)
198+
}
199+
200+
/// Divide a measurement by a scalar value.
201+
/// - returns: A measurement of value `lhs.value / rhs` with the same unit as `lhs`.
202+
public static func /(lhs: Measurement<UnitType>, rhs: Double) -> Measurement<UnitType> {
203+
return Measurement(value: lhs.value / rhs, unit: lhs.unit)
204+
}
205+
206+
/// Divide a scalar value by a measurement.
207+
/// - returns: A measurement of value `lhs / rhs.value` with the same unit as `rhs`.
208+
public static func /(lhs: Double, rhs: Measurement<UnitType>) -> Measurement<UnitType> {
209+
return Measurement(value: lhs / rhs.value, unit: rhs.unit)
210+
}
211+
212+
/// Compare two measurements of the same `Unit`.
213+
/// - returns: `true` if `lhs.value == rhs.value && lhs.unit == rhs.unit`.
214+
public static func ==(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Bool {
215+
return lhs.value == rhs.value && lhs.unit == rhs.unit
216+
}
217+
218+
/// Compare two measurements of the same `Unit`.
219+
/// - note: This function does not check `==` for the `unit` property of `lhs` and `rhs`.
220+
/// - returns: `lhs.value < rhs.value`
221+
public static func <(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Bool {
222+
return lhs.value < rhs.value
223+
}
224+
225+
/// Compare two measurements of the same `Unit`.
226+
/// - note: This function does not check `==` for the `unit` property of `lhs` and `rhs`.
227+
/// - returns: `lhs.value > rhs.value`
228+
public static func >(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Bool {
229+
return lhs.value > rhs.value
230+
}
231+
232+
/// Compare two measurements of the same `Unit`.
233+
/// - note: This function does not check `==` for the `unit` property of `lhs` and `rhs`.
234+
/// - returns: `lhs.value <= rhs.value`
235+
public static func <=(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Bool {
236+
return lhs.value <= rhs.value
237+
}
238+
239+
/// Compare two measurements of the same `Unit`.
240+
/// - note: This function does not check `==` for the `unit` property of `lhs` and `rhs`.
241+
/// - returns: `lhs.value >= rhs.value`
242+
public static func >=(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Bool {
243+
return lhs.value >= rhs.value
244+
}
245+
}
246+
276247
// Implementation note: similar to NSArray, NSDictionary, etc., NSMeasurement's import as an ObjC generic type is suppressed by the importer. Eventually we will need a more general purpose mechanism to correctly import generic types.
277248

278249
extension Measurement : _ObjectTypeBridgeable {

0 commit comments

Comments
 (0)