Skip to content

Instantly share code, notes, and snippets.

@erica
Created July 27, 2015 20:10
Show Gist options
  • Save erica/2f6a38c844573c778b0f to your computer and use it in GitHub Desktop.
Save erica/2f6a38c844573c778b0f to your computer and use it in GitHub Desktop.
//: 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)}}
@AmitaiB
Copy link

AmitaiB commented Feb 13, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment