Skip to content

Commit c95aa8c

Browse files
committed
[gardening][Measurement] Move operators into types
1 parent 03e2846 commit c95aa8c

File tree

1 file changed

+158
-130
lines changed

1 file changed

+158
-130
lines changed

Foundation/Measurement.swift

Lines changed: 158 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -72,176 +72,204 @@ extension Measurement where UnitType : Dimension {
7272

7373
}
7474

75-
/// Add two measurements of the same Unit.
76-
/// - precondition: The `unit` of `lhs` and `rhs` must be `isEqual`.
77-
/// - returns: A measurement of value `lhs.value + rhs.value` and unit `lhs.unit`.
78-
public func +<UnitType>(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Measurement<UnitType> {
79-
if lhs.unit.isEqual(rhs.unit) {
80-
return Measurement(value: lhs.value + rhs.value, unit: lhs.unit)
81-
} else {
82-
fatalError("Attempt to add measurements with non-equal units")
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+
}
8385
}
8486
}
8587

86-
/// Add two measurements of the same Dimension.
87-
///
88-
/// 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.
89-
/// - returns: The result of adding the two measurements.
90-
public func +<UnitType : Dimension>(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Measurement<UnitType> {
91-
if lhs.unit.isEqual(rhs.unit) {
92-
return Measurement(value: lhs.value + rhs.value, unit: lhs.unit)
93-
} else {
94-
let lhsValueInTermsOfBase = lhs.unit.converter.baseUnitValue(fromValue: lhs.value)
95-
let rhsValueInTermsOfBase = rhs.unit.converter.baseUnitValue(fromValue: rhs.value)
96-
return Measurement(value: lhsValueInTermsOfBase + rhsValueInTermsOfBase, unit: type(of: lhs.unit).baseUnit())
88+
extension Measurement where UnitType : Dimension {
89+
/// Add two measurements of the same Dimension.
90+
///
91+
/// 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.
92+
/// - returns: The result of adding the two measurements.
93+
public static func +(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Measurement<UnitType> {
94+
if lhs.unit.isEqual(rhs.unit) {
95+
return Measurement(value: lhs.value + rhs.value, unit: lhs.unit)
96+
} else {
97+
let lhsValueInTermsOfBase = lhs.unit.converter.baseUnitValue(fromValue: lhs.value)
98+
let rhsValueInTermsOfBase = rhs.unit.converter.baseUnitValue(fromValue: rhs.value)
99+
return Measurement(value: lhsValueInTermsOfBase + rhsValueInTermsOfBase, unit: type(of: lhs.unit).baseUnit())
100+
}
97101
}
98102
}
99103

100-
/// Subtract two measurements of the same Unit.
101-
/// - precondition: The `unit` of `lhs` and `rhs` must be `isEqual`.
102-
/// - returns: A measurement of value `lhs.value - rhs.value` and unit `lhs.unit`.
103-
public func -<UnitType>(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Measurement<UnitType> {
104-
if lhs.unit.isEqual(rhs.unit) {
105-
return Measurement(value: lhs.value - rhs.value, unit: lhs.unit)
106-
} else {
107-
fatalError("Attempt to subtract measurements with non-equal units")
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+
}
108114
}
109115
}
110116

111-
/// Subtract two measurements of the same Dimension.
112-
///
113-
/// 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.
114-
/// - returns: The result of adding the two measurements.
115-
public func -<UnitType : Dimension>(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Measurement<UnitType> {
116-
if lhs.unit == rhs.unit {
117-
return Measurement(value: lhs.value - rhs.value, unit: lhs.unit)
118-
} else {
119-
let lhsValueInTermsOfBase = lhs.unit.converter.baseUnitValue(fromValue: lhs.value)
120-
let rhsValueInTermsOfBase = rhs.unit.converter.baseUnitValue(fromValue: rhs.value)
121-
return Measurement(value: lhsValueInTermsOfBase - rhsValueInTermsOfBase, unit: type(of: lhs.unit).baseUnit())
117+
extension Measurement where UnitType : Dimension {
118+
/// Subtract two measurements of the same Dimension.
119+
///
120+
/// 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.
121+
/// - returns: The result of adding the two measurements.
122+
public static func -(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Measurement<UnitType> {
123+
if lhs.unit == rhs.unit {
124+
return Measurement(value: lhs.value - rhs.value, unit: lhs.unit)
125+
} else {
126+
let lhsValueInTermsOfBase = lhs.unit.converter.baseUnitValue(fromValue: lhs.value)
127+
let rhsValueInTermsOfBase = rhs.unit.converter.baseUnitValue(fromValue: rhs.value)
128+
return Measurement(value: lhsValueInTermsOfBase - rhsValueInTermsOfBase, unit: type(of: lhs.unit).baseUnit())
129+
}
122130
}
123131
}
124132

125-
/// Multiply a measurement by a scalar value.
126-
/// - returns: A measurement of value `lhs.value * rhs` with the same unit as `lhs`.
127-
public func *<UnitType>(lhs: Measurement<UnitType>, rhs: Double) -> Measurement<UnitType> {
128-
return Measurement(value: lhs.value * rhs, unit: lhs.unit)
129-
}
130-
131-
/// Multiply a scalar value by a measurement.
132-
/// - returns: A measurement of value `lhs * rhs.value` with the same unit as `rhs`.
133-
public func *<UnitType>(lhs: Double, rhs: Measurement<UnitType>) -> Measurement<UnitType> {
134-
return Measurement(value: lhs * rhs.value, unit: rhs.unit)
135-
}
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+
}
136139

137-
/// Divide a measurement by a scalar value.
138-
/// - returns: A measurement of value `lhs.value / rhs` with the same unit as `lhs`.
139-
public func /<UnitType>(lhs: Measurement<UnitType>, rhs: Double) -> Measurement<UnitType> {
140-
return Measurement(value: lhs.value / rhs, unit: lhs.unit)
141-
}
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+
}
142145

143-
/// Divide a scalar value by a measurement.
144-
/// - returns: A measurement of value `lhs / rhs.value` with the same unit as `rhs`.
145-
public func /<UnitType>(lhs: Double, rhs: Measurement<UnitType>) -> Measurement<UnitType> {
146-
return Measurement(value: lhs / rhs.value, unit: rhs.unit)
147-
}
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+
}
148151

149-
/// Compare two measurements of the same `Unit`.
150-
/// - returns: `true` if `lhs.value == rhs.value && lhs.unit == rhs.unit`.
151-
public func ==<UnitType>(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Bool {
152-
return lhs.value == rhs.value && lhs.unit == rhs.unit
153-
}
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+
}
154157

155-
/// Compare two measurements of the same `Dimension`.
156-
///
157-
/// 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.
158-
/// - returns: `true` if the measurements are equal.
159-
public func ==<UnitType : Dimension>(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Bool {
160-
if lhs.unit == rhs.unit {
161-
return lhs.value == rhs.value
162-
} else {
163-
let rhsInLhs = rhs.converted(to: lhs.unit)
164-
return lhs.value == rhsInLhs.value
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
165162
}
166163
}
167164

168-
/// Compare two measurements of the same `Unit`.
169-
/// - note: This function does not check `==` for the `unit` property of `lhs` and `rhs`.
170-
/// - returns: `lhs.value < rhs.value`
171-
public func <<UnitType>(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Bool {
172-
return lhs.value < rhs.value
165+
extension Measurement where UnitType : Dimension {
166+
/// Compare two measurements of the same `Dimension`.
167+
///
168+
/// 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.
169+
/// - returns: `true` if the measurements are equal.
170+
public static func ==(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Bool {
171+
if lhs.unit == rhs.unit {
172+
return lhs.value == rhs.value
173+
} else {
174+
let rhsInLhs = rhs.converted(to: lhs.unit)
175+
return lhs.value == rhsInLhs.value
176+
}
177+
}
173178
}
174179

175-
/// Compare two measurements of the same `Dimension`.
176-
///
177-
/// 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.
178-
/// - returns: `true` if `lhs` is less than `rhs`.
179-
public func <<UnitType : Dimension>(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Bool {
180-
if lhs.unit == rhs.unit {
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 {
181185
return lhs.value < rhs.value
182-
} else {
183-
let rhsInLhs = rhs.converted(to: lhs.unit)
184-
return lhs.value < rhsInLhs.value
185186
}
186187
}
187188

188-
/// Compare two measurements of the same `Unit`.
189-
/// - note: This function does not check `==` for the `unit` property of `lhs` and `rhs`.
190-
/// - returns: `lhs.value > rhs.value`
191-
public func ><UnitType>(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Bool {
192-
return lhs.value > rhs.value
189+
extension Measurement where UnitType : Dimension {
190+
/// Compare two measurements of the same `Dimension`.
191+
///
192+
/// 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.
193+
/// - returns: `true` if `lhs` is less than `rhs`.
194+
public static func <(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Bool {
195+
if lhs.unit == rhs.unit {
196+
return lhs.value < rhs.value
197+
} else {
198+
let rhsInLhs = rhs.converted(to: lhs.unit)
199+
return lhs.value < rhsInLhs.value
200+
}
201+
}
193202
}
194203

195-
/// Compare two measurements of the same `Dimension`.
196-
///
197-
/// 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.
198-
/// - returns: `true` if `lhs` is greater than `rhs`.
199-
public func ><UnitType : Dimension>(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Bool {
200-
if lhs.unit == rhs.unit {
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 {
201209
return lhs.value > rhs.value
202-
} else {
203-
let rhsInLhs = rhs.converted(to: lhs.unit)
204-
return lhs.value > rhsInLhs.value
205210
}
206211
}
207212

208-
/// Compare two measurements of the same `Unit`.
209-
/// - note: This function does not check `==` for the `unit` property of `lhs` and `rhs`.
210-
/// - returns: `lhs.value <= rhs.value`
211-
public func <=<UnitType>(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Bool {
212-
return lhs.value <= rhs.value
213+
extension Measurement where UnitType : Dimension {
214+
/// Compare two measurements of the same `Dimension`.
215+
///
216+
/// 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.
217+
/// - returns: `true` if `lhs` is greater than `rhs`.
218+
public static func >(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Bool {
219+
if lhs.unit == rhs.unit {
220+
return lhs.value > rhs.value
221+
} else {
222+
let rhsInLhs = rhs.converted(to: lhs.unit)
223+
return lhs.value > rhsInLhs.value
224+
}
225+
}
213226
}
214227

215-
/// Compare two measurements of the same `Dimension`.
216-
///
217-
/// 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.
218-
/// - returns: `true` if `lhs` is less than or equal to `rhs`.
219-
public func <=<UnitType : Dimension>(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Bool {
220-
if lhs.unit == rhs.unit {
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 {
221233
return lhs.value <= rhs.value
222-
} else {
223-
let rhsInLhs = rhs.converted(to: lhs.unit)
224-
return lhs.value <= rhsInLhs.value
225234
}
226235
}
227236

228-
/// Compare two measurements of the same `Unit`.
229-
/// - note: This function does not check `==` for the `unit` property of `lhs` and `rhs`.
230-
/// - returns: `lhs.value >= rhs.value`
231-
public func >=<UnitType>(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Bool {
232-
return lhs.value >= rhs.value
237+
extension Measurement where UnitType : Dimension {
238+
/// Compare two measurements of the same `Dimension`.
239+
///
240+
/// 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.
241+
/// - returns: `true` if `lhs` is less than or equal to `rhs`.
242+
public static func <=(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Bool {
243+
if lhs.unit == rhs.unit {
244+
return lhs.value <= rhs.value
245+
} else {
246+
let rhsInLhs = rhs.converted(to: lhs.unit)
247+
return lhs.value <= rhsInLhs.value
248+
}
249+
}
233250
}
234251

235-
/// Compare two measurements of the same `Dimension`.
236-
///
237-
/// 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.
238-
/// - returns: `true` if `lhs` is greater or equal to `rhs`.
239-
public func >=<UnitType : Dimension>(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Bool {
240-
if lhs.unit == rhs.unit {
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 {
241257
return lhs.value >= rhs.value
242-
} else {
243-
let rhsInLhs = rhs.converted(to: lhs.unit)
244-
return lhs.value >= rhsInLhs.value
258+
}
259+
}
260+
261+
extension Measurement where UnitType : Dimension {
262+
/// Compare two measurements of the same `Dimension`.
263+
///
264+
/// 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.
265+
/// - returns: `true` if `lhs` is greater or equal to `rhs`.
266+
public static func >=(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Bool {
267+
if lhs.unit == rhs.unit {
268+
return lhs.value >= rhs.value
269+
} else {
270+
let rhsInLhs = rhs.converted(to: lhs.unit)
271+
return lhs.value >= rhsInLhs.value
272+
}
245273
}
246274
}
247275

0 commit comments

Comments
 (0)