-
-
Save dtorres/e61ed3b463a469ea6f6b99aeff3aae8a to your computer and use it in GitHub Desktop.
Conversions in Measurement too accurate for you? Here take this…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension Measurement where UnitType: UnitLength { | |
func approximatelyConverted(to otherUnit: UnitType) -> Measurement<UnitType> { | |
switch (unit, otherUnit) { | |
case (UnitLength.meters, UnitLength.feet): | |
return Measurement(value: value * 3, unit: otherUnit) | |
case (UnitLength.feet, UnitLength.meters): | |
return Measurement(value: value * 3 / 10, unit: otherUnit) | |
default: | |
return converted(to: otherUnit) | |
} | |
} | |
} | |
extension Measurement where UnitType: UnitSpeed { | |
func approximatelyConverted(to otherUnit: UnitType) -> Measurement<UnitType> { | |
switch (unit, otherUnit) { | |
case (UnitSpeed.knots, UnitSpeed.metersPerSecond): | |
return Measurement(value: value * 2.0, unit: otherUnit) | |
case (UnitSpeed.metersPerSecond, UnitSpeed.knots): | |
return Measurement(value: value / 2.0, unit: otherUnit) | |
default: | |
return converted(to: otherUnit) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment