generics - Swift - is there a standard protocol that defines +,-,*,/ functions, which is adopted by all "arithmetic" types like Double, Float, Int etc.? -


i want write generic function return sum of it's 2 parameters, 1 below:

func add<t: ???>(left: t, right: t) -> t {     return left+right } 

of course, in order use + operator, t type needs conform protocol defines + operator.

in case of several other operators, there built in protocols - e.g. equatable ==, , comparable <, > etc. protocols adopted swift's built in "arithmetic" types double, float, int16 etc.

is there standard protocol defines +,-,*,/ operators, adopted "arithmetic" types double, float, int, uint, int16 etc.?

there isn't in library that, mean. can though:

protocol arithmetic {     func +(lhs: self, rhs: self) -> self     func -(lhs: self, rhs: self) -> self     func *(lhs: self, rhs: self) -> self     func /(lhs: self, rhs: self) -> self }  extension int8 : arithmetic {} extension int16 : arithmetic {} extension int32 : arithmetic {} extension int64 : arithmetic {}  extension uint8 : arithmetic {} extension uint16 : arithmetic {} extension uint32 : arithmetic {} extension uint64 : arithmetic {}  extension float80 : arithmetic {} extension float : arithmetic {} extension double : arithmetic {}   func add<t: arithmetic>(a: t, b: t) -> t {     return + b }  add(3, b: 4) 

Comments