Skip to content

Instantly share code, notes, and snippets.

@fcanas
Created May 21, 2015 15:11
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save fcanas/6f02fc92e3ae0b2f9ce7 to your computer and use it in GitHub Desktop.
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