I want to write a generalized function that returns the sum of two parameters, such as:
func add<T: ???>(left: T, right: T) -> T {
return left+right
}
Of course, to use an operator, the +type Tmust conform to the protocol defining the operator +.
In the case of several other operators, there are built-in protocols - for example, Equatablefor ==and Comparablefor <, >etc. These protocols are accepted by all Swift built into arithmetic types such as Double, Float, Int16, etc.
Is there a standard protocol that defines the operators +, -, *, /which are accepted by all "arithmetic" types such as Double, Float, Int, UInt, Int16 , etc.?
source
share