I want to write a generic function that will return a sum of it's two parameters, like the one below:
func add<T: ???>(left: T, right: T) -> T {
return left+right
}
Of course, in order to use +
operator, T
type needs to conform to a protocol that defines +
operator.
In case of several other operators, there are built in protocols - e.g. Equatable
for ==
, and Comparable
for <
, >
etc. Those protocols are adopted by all Swift's built in "arithmetic" types like Double, Float, Int16 etc.
Is there a standard protocol that defines +
,-
,*
,/
operators, which is adopted by ALL "arithmetic" types like Double, Float, Int, UInt, Int16 etc.?