-
-
Save beccadax/c6832e4d095877207661 to your computer and use it in GitHub Desktop.
Creating a Numeric Type : Distance
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
public struct Distance :NumericType { | |
public var value :Double | |
public init(_ value: Double) { | |
self.value = value | |
} | |
} | |
extension Distance :IntegerLiteralConvertible { | |
public init(integerLiteral: IntegerLiteralType) { | |
self.init(Double(integerLiteral)) | |
} | |
} | |
extension Distance :FloatLiteralConvertible { | |
public init(floatLiteral: FloatLiteralType) { | |
self.init(Double(floatLiteral)) | |
} | |
} |
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
public protocol NumericType : Comparable, FloatLiteralConvertible, IntegerLiteralConvertible, SignedNumberType { | |
var value :Double { set get } | |
init(_ value: Double) | |
} | |
public func % <T :NumericType> (lhs: T, rhs: T) -> T { | |
return T(lhs.value % rhs.value) | |
} | |
public func + <T :NumericType> (lhs: T, rhs: T) -> T { | |
return T(lhs.value + rhs.value) | |
} | |
public func - <T :NumericType> (lhs: T, rhs: T) -> T { | |
return T(lhs.value - rhs.value) | |
} | |
public func < <T :NumericType> (lhs: T, rhs: T) -> Bool { | |
return lhs.value < rhs.value | |
} | |
public func == <T :NumericType> (lhs: T, rhs: T) -> Bool { | |
return lhs.value == rhs.value | |
} | |
public prefix func - <T: NumericType> (number: T) -> T { | |
return T(-number.value) | |
} | |
public func += <T :NumericType> (inout lhs: T, rhs: T) { | |
lhs.value = lhs.value + rhs.value | |
} | |
public func -= <T :NumericType> (inout lhs: T, rhs: T) { | |
lhs.value = lhs.value - rhs.value | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment