Skip to content

Instantly share code, notes, and snippets.

@beccadax
Forked from fcanas/Distance.swift
Last active August 29, 2015 14:25
Show Gist options
  • Save beccadax/c6832e4d095877207661 to your computer and use it in GitHub Desktop.
Save beccadax/c6832e4d095877207661 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