I have one extension which helps me to range bound a value with min and max. There are different scenarios where I need to use a different type like Int, Float, Double, CGFloat. So for that, I have created multiple extensions like below.
extension Int {
func clamp(min: Int, _ max: Int) -> Int {
return Swift.max(min, Swift.min(max, self))
}
}
extension CGFloat {
func clamp (min: CGFloat, _ max: CGFloat) -> CGFloat {
return Swift.max(min, Swift.min(max, self))
}
}
So, how can I make it more generic by using generic type T which only confirms to take numeric values?