0

I want to add default implementation for Int/Float/Double value.

protocol ValueLimit { associatedtype T where T: Comparable func limit(from minValue: T, to maxValue: T) -> T } extension ValueLimit { func limit(from minValue: T, to maxValue: T) -> T { return Swift.max(Swift.min(self as! Self.T, maxValue), minValue) } } extension Int: ValueLimit { typealias T = Int } extension Float: ValueLimit { typealias T = Float } extension Double: ValueLimit { typealias T = Double } 

I belive there is a better way, which can simply use empty extension extension Int: ValueLimit {} to do this, without typealias T = Int, but I don't know how to do this.

2 Answers 2

2

There's no need for an associated type, you can simply use Self in your limit declaration. You can also declare that conforming types need to conform to Comparable as well in the protocol declaration.

Self refers to the current type, so when using it in protocol methods, it refers to the conforming type. This is how standard library protocols such as Equatable and Comparable are implemented as well.

protocol ValueLimit: Comparable { func limit(from minValue: Self, to maxValue: Self) -> Self } extension ValueLimit { func limit(from minValue: Self, to maxValue: Self) -> Self { Swift.max(Swift.min(self, maxValue), minValue) } } extension Int: ValueLimit {} extension Float: ValueLimit {} extension Double: ValueLimit {} 
Sign up to request clarification or add additional context in comments.

Comments

1

Maybe this would be simpler.

extension Comparable { func limit(from minValue: Self, to maxValue: Self) -> Self { return Swift.max(Swift.min(self, maxValue), minValue) } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.