Created
October 20, 2015 14:47
-
-
Save tal/79207e65eca296f1bef3 to your computer and use it in GitHub Desktop.
I would like to define variables whos type is a protocol but limited such that the internal type is of a specific type.
This file contains hidden or 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
struct DoubleRange:Scalable { | |
typealias ScalableType = Double | |
let start:Double | |
let end:Double | |
func scale(dub: Double) -> Double? { | |
return start + (dub * diff) | |
} | |
func scale(dub:Double) -> CGFloat? { | |
if let sc:Double = scale(dub) { | |
return CGFloat(sc) | |
} | |
return nil | |
} | |
} |
This file contains hidden or 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
/** | |
* Defines an object that can scale some internal value on infinite slices between `start` and `end`. | |
* The value is scaled from 0-1. | |
*/ | |
protocol Scalable { | |
typealias ScalableType | |
var start:ScalableType { get } | |
var end:ScalableType { get } | |
func scale(dub:Double) -> ScalableType? | |
func series(count count:Int) -> [ScalableType?] | |
} | |
extension Scalable { | |
func series(count count:Int) -> [ScalableType?] { | |
return (0..<count).map({ Double($0)/Double(count) }).map(scale) | |
} | |
func scalingFactor(progress progress:Double) -> Double { | |
return progress | |
} | |
} | |
extension Scalable where ScalableType:SignedNumberType { | |
var diff:ScalableType { | |
return end - start | |
} | |
} |
This file contains hidden or 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
class Foo { | |
// Basically I want any scaler who's internal type is a `Double`. I don't care the implementation. | |
var scaler:Scalable<where Scalable.ScalableType:Double> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment