From 504cad7ac61f99f6098c6a53e0a92c42a84ec2c5 Mon Sep 17 00:00:00 2001 From: Sho Ikeda Date: Fri, 1 Dec 2017 09:29:32 +0900 Subject: [PATCH] [Measurement] Group operators into just two extensions --- Foundation/Measurement.swift | 189 +++++++++++++++-------------------- 1 file changed, 80 insertions(+), 109 deletions(-) diff --git a/Foundation/Measurement.swift b/Foundation/Measurement.swift index 846691fdc0..c5191edd8b 100644 --- a/Foundation/Measurement.swift +++ b/Foundation/Measurement.swift @@ -69,23 +69,7 @@ extension Measurement where UnitType : Dimension { public mutating func convert(to otherUnit: UnitType) { self = converted(to: otherUnit) } - -} - -extension Measurement { - /// Add two measurements of the same Unit. - /// - precondition: The `unit` of `lhs` and `rhs` must be `isEqual`. - /// - returns: A measurement of value `lhs.value + rhs.value` and unit `lhs.unit`. - public static func +(lhs: Measurement, rhs: Measurement) -> Measurement { - if lhs.unit.isEqual(rhs.unit) { - return Measurement(value: lhs.value + rhs.value, unit: lhs.unit) - } else { - fatalError("Attempt to add measurements with non-equal units") - } - } -} -extension Measurement where UnitType : Dimension { /// Add two measurements of the same Dimension. /// /// 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 { return Measurement(value: lhsValueInTermsOfBase + rhsValueInTermsOfBase, unit: type(of: lhs.unit).baseUnit()) } } -} -extension Measurement { - /// Subtract two measurements of the same Unit. - /// - precondition: The `unit` of `lhs` and `rhs` must be `isEqual`. - /// - returns: A measurement of value `lhs.value - rhs.value` and unit `lhs.unit`. - public static func -(lhs: Measurement, rhs: Measurement) -> Measurement { - if lhs.unit.isEqual(rhs.unit) { - return Measurement(value: lhs.value - rhs.value, unit: lhs.unit) - } else { - fatalError("Attempt to subtract measurements with non-equal units") - } - } -} - -extension Measurement where UnitType : Dimension { /// Subtract two measurements of the same Dimension. /// /// 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 { return Measurement(value: lhsValueInTermsOfBase - rhsValueInTermsOfBase, unit: type(of: lhs.unit).baseUnit()) } } -} - -extension Measurement { - /// Multiply a measurement by a scalar value. - /// - returns: A measurement of value `lhs.value * rhs` with the same unit as `lhs`. - public static func *(lhs: Measurement, rhs: Double) -> Measurement { - return Measurement(value: lhs.value * rhs, unit: lhs.unit) - } - - /// Multiply a scalar value by a measurement. - /// - returns: A measurement of value `lhs * rhs.value` with the same unit as `rhs`. - public static func *(lhs: Double, rhs: Measurement) -> Measurement { - return Measurement(value: lhs * rhs.value, unit: rhs.unit) - } - - /// Divide a measurement by a scalar value. - /// - returns: A measurement of value `lhs.value / rhs` with the same unit as `lhs`. - public static func /(lhs: Measurement, rhs: Double) -> Measurement { - return Measurement(value: lhs.value / rhs, unit: lhs.unit) - } - - /// Divide a scalar value by a measurement. - /// - returns: A measurement of value `lhs / rhs.value` with the same unit as `rhs`. - public static func /(lhs: Double, rhs: Measurement) -> Measurement { - return Measurement(value: lhs / rhs.value, unit: rhs.unit) - } - - /// Compare two measurements of the same `Unit`. - /// - returns: `true` if `lhs.value == rhs.value && lhs.unit == rhs.unit`. - public static func ==(lhs: Measurement, rhs: Measurement) -> Bool { - return lhs.value == rhs.value && lhs.unit == rhs.unit - } -} -extension Measurement where UnitType : Dimension { /// Compare two measurements of the same `Dimension`. /// /// 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 { return lhs.value == rhsInLhs.value } } -} -extension Measurement { - /// Compare two measurements of the same `Unit`. - /// - note: This function does not check `==` for the `unit` property of `lhs` and `rhs`. - /// - returns: `lhs.value < rhs.value` - public static func <(lhs: Measurement, rhs: Measurement) -> Bool { - return lhs.value < rhs.value - } -} - -extension Measurement where UnitType : Dimension { /// Compare two measurements of the same `Dimension`. /// /// 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 { return lhs.value < rhsInLhs.value } } -} -extension Measurement { - /// Compare two measurements of the same `Unit`. - /// - note: This function does not check `==` for the `unit` property of `lhs` and `rhs`. - /// - returns: `lhs.value > rhs.value` - public static func >(lhs: Measurement, rhs: Measurement) -> Bool { - return lhs.value > rhs.value - } -} - -extension Measurement where UnitType : Dimension { /// Compare two measurements of the same `Dimension`. /// /// 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 { return lhs.value > rhsInLhs.value } } -} -extension Measurement { - /// Compare two measurements of the same `Unit`. - /// - note: This function does not check `==` for the `unit` property of `lhs` and `rhs`. - /// - returns: `lhs.value <= rhs.value` - public static func <=(lhs: Measurement, rhs: Measurement) -> Bool { - return lhs.value <= rhs.value - } -} - -extension Measurement where UnitType : Dimension { /// Compare two measurements of the same `Dimension`. /// /// 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 { return lhs.value <= rhsInLhs.value } } -} - -extension Measurement { - /// Compare two measurements of the same `Unit`. - /// - note: This function does not check `==` for the `unit` property of `lhs` and `rhs`. - /// - returns: `lhs.value >= rhs.value` - public static func >=(lhs: Measurement, rhs: Measurement) -> Bool { - return lhs.value >= rhs.value - } -} -extension Measurement where UnitType : Dimension { /// Compare two measurements of the same `Dimension`. /// /// 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 { } } +extension Measurement { + /// Add two measurements of the same Unit. + /// - precondition: The `unit` of `lhs` and `rhs` must be `isEqual`. + /// - returns: A measurement of value `lhs.value + rhs.value` and unit `lhs.unit`. + public static func +(lhs: Measurement, rhs: Measurement) -> Measurement { + if lhs.unit.isEqual(rhs.unit) { + return Measurement(value: lhs.value + rhs.value, unit: lhs.unit) + } else { + fatalError("Attempt to add measurements with non-equal units") + } + } + + /// Subtract two measurements of the same Unit. + /// - precondition: The `unit` of `lhs` and `rhs` must be `isEqual`. + /// - returns: A measurement of value `lhs.value - rhs.value` and unit `lhs.unit`. + public static func -(lhs: Measurement, rhs: Measurement) -> Measurement { + if lhs.unit.isEqual(rhs.unit) { + return Measurement(value: lhs.value - rhs.value, unit: lhs.unit) + } else { + fatalError("Attempt to subtract measurements with non-equal units") + } + } + + public static func *(lhs: Measurement, rhs: Double) -> Measurement { + return Measurement(value: lhs.value * rhs, unit: lhs.unit) + } + + /// Multiply a scalar value by a measurement. + /// - returns: A measurement of value `lhs * rhs.value` with the same unit as `rhs`. + public static func *(lhs: Double, rhs: Measurement) -> Measurement { + return Measurement(value: lhs * rhs.value, unit: rhs.unit) + } + + /// Divide a measurement by a scalar value. + /// - returns: A measurement of value `lhs.value / rhs` with the same unit as `lhs`. + public static func /(lhs: Measurement, rhs: Double) -> Measurement { + return Measurement(value: lhs.value / rhs, unit: lhs.unit) + } + + /// Divide a scalar value by a measurement. + /// - returns: A measurement of value `lhs / rhs.value` with the same unit as `rhs`. + public static func /(lhs: Double, rhs: Measurement) -> Measurement { + return Measurement(value: lhs / rhs.value, unit: rhs.unit) + } + + /// Compare two measurements of the same `Unit`. + /// - returns: `true` if `lhs.value == rhs.value && lhs.unit == rhs.unit`. + public static func ==(lhs: Measurement, rhs: Measurement) -> Bool { + return lhs.value == rhs.value && lhs.unit == rhs.unit + } + + /// Compare two measurements of the same `Unit`. + /// - note: This function does not check `==` for the `unit` property of `lhs` and `rhs`. + /// - returns: `lhs.value < rhs.value` + public static func <(lhs: Measurement, rhs: Measurement) -> Bool { + return lhs.value < rhs.value + } + + /// Compare two measurements of the same `Unit`. + /// - note: This function does not check `==` for the `unit` property of `lhs` and `rhs`. + /// - returns: `lhs.value > rhs.value` + public static func >(lhs: Measurement, rhs: Measurement) -> Bool { + return lhs.value > rhs.value + } + + /// Compare two measurements of the same `Unit`. + /// - note: This function does not check `==` for the `unit` property of `lhs` and `rhs`. + /// - returns: `lhs.value <= rhs.value` + public static func <=(lhs: Measurement, rhs: Measurement) -> Bool { + return lhs.value <= rhs.value + } + + /// Compare two measurements of the same `Unit`. + /// - note: This function does not check `==` for the `unit` property of `lhs` and `rhs`. + /// - returns: `lhs.value >= rhs.value` + public static func >=(lhs: Measurement, rhs: Measurement) -> Bool { + return lhs.value >= rhs.value + } +} + // 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. extension Measurement : _ObjectTypeBridgeable {