Skip to content

Instantly share code, notes, and snippets.

@tal
Created October 20, 2015 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tal/79207e65eca296f1bef3 to your computer and use it in GitHub Desktop.
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.
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
}
}
/**
* 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
}
}
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