Created
July 27, 2015 20:10
-
-
Save erica/2f6a38c844573c778b0f to your computer and use it in GitHub Desktop.
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
//: Recipe X-1 | |
//: Numbers that can be represented as Double instances | |
public protocol DoubleRepresentable { | |
var doubleValue: Double {get} | |
} | |
//: Numbers that convert to other types | |
public protocol ConvertibleNumberType: DoubleRepresentable {} | |
public extension ConvertibleNumberType { | |
public var floatValue: Float {get {return Float(doubleValue)}} | |
public var intValue: Int {get {return lrint(doubleValue)}} | |
public var CGFloatValue: CGFloat {get {return CGFloat(doubleValue)}} | |
} | |
// Double Representable Conformance | |
extension Double: ConvertibleNumberType {public var doubleValue: Double {return self}} | |
extension Int: ConvertibleNumberType {public var doubleValue: Double {return Double(self)}} | |
extension CGFloat: ConvertibleNumberType {public var doubleValue: Double {return Double(self)}} | |
extension Float: ConvertibleNumberType {public var doubleValue: Double {return Double(self)}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was wondering if you could spare a moment to look at my expansion on this snippet of yours (my gist). Do you still think this sort of thing is useful, asking for trouble, etc?
Gratitude.