Created
August 16, 2015 23:48
-
-
Save Nadohs/c86990ccbe9ddeea12a6 to your computer and use it in GitHub Desktop.
Dot Property Number Casting
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
//Number classes use initializers, that take in other number types to convert between types | |
//Essentially this is what is going on: | |
//`Int(4.5)` ~is just this~ `Int.init(4.5)` | |
//Which is using `init(_ value:Double)` definid with in the `Int` class | |
//Using this knoledge we can optimize our conersions with pattern matching | |
import CoreGraphics //For CGFloat | |
public protocol NumberConvertible:Equatable,Comparable{ | |
init(_ value: Int) | |
init(_ value: Float) | |
init(_ value: Double) | |
init(_ value: CGFloat) | |
init(_ value: UInt8) | |
init(_ value: Int8) | |
init(_ value: UInt16) | |
init(_ value: Int16) | |
init(_ value: UInt32) | |
init(_ value: Int32) | |
init(_ value: UInt64) | |
init(_ value: Int64) | |
init(_ value: UInt) | |
} | |
public protocol IntNumberConvertible : NumberConvertible { | |
static var max: Self { get } | |
} | |
//CGFloat does not have a init() case that excepts another CGFloat, | |
//So we add one, that converts to a Double, and uses init(_ value:Double) | |
extension CGFloat{ | |
public init(_ value: CGFloat){ | |
self.init(Double(value)) | |
} | |
} | |
extension NumberConvertible { | |
private func convert<T: NumberConvertible>() -> T { | |
switch self { | |
case let x as CGFloat: | |
return T(x) | |
case let x as Float: | |
return T(x) | |
case let x as Int: | |
return T(x) | |
case let x as Double: | |
return T(x) | |
case let x as UInt8: | |
return T(x) | |
case let x as Int8: | |
return T(x) | |
case let x as UInt16: | |
return T(x) | |
case let x as Int16: | |
return T(x) | |
case let x as UInt32: | |
return T(x) | |
case let x as Int32: | |
return T(x) | |
case let x as UInt64: | |
return T(x) | |
case let x as Int64: | |
return T(x) | |
case let x as UInt: | |
return T(x) | |
default: | |
assert(false, "NumberConvertible convert cast failed!") | |
return T(0) | |
} | |
} | |
public var c:CGFloat{ | |
get{ return convert() } | |
} | |
public var f:Float{ | |
get{ return convert() } | |
} | |
public var d:Double{ | |
get{ return convert() } | |
} | |
public var i:Int{ | |
get{ return convert() } | |
} | |
} | |
extension CGFloat : NumberConvertible {} | |
extension Double : NumberConvertible {} | |
extension Float : NumberConvertible {} | |
extension Int : IntNumberConvertible {} | |
extension UInt8 : IntNumberConvertible {} | |
extension Int8 : IntNumberConvertible {} | |
extension UInt16 : IntNumberConvertible {} | |
extension Int16 : IntNumberConvertible {} | |
extension UInt32 : IntNumberConvertible {} | |
extension Int32 : IntNumberConvertible {} | |
extension UInt64 : IntNumberConvertible {} | |
extension Int64 : IntNumberConvertible {} | |
extension UInt : IntNumberConvertible {} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment