Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Creating a Numeric Type : Distance
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))
}
}
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