7

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.?

Grzegorz D.
  • 1,748
  • 15
  • 18
  • 2
    What you're doing is exactly how to do it. It isn't clear what answer you're looking for; your question isn't really a question, more of a moan... – matt Jul 09 '15 at 18:06
  • Why should `Double` conform to `IntegerArithmeticType`? A Double can represent something that is not an Integer. – Luca Angeletti Jul 09 '15 at 18:12
  • can you please improve this question, or remove it? -> http://stackoverflow.com/help/how-to-ask – fqdn Jul 09 '15 at 18:19
  • What is the problem of the question? He's asking if there's a better way. – User Jul 09 '15 at 19:02
  • Ok, I rewrote the question to make it more clear. – Grzegorz D. Jul 09 '15 at 20:31
  • Ok, I understand your question from a theoretical point of view. But in practice, what is your purpose? You already have the `add` function defined for all these types, and you can invoke it using the `+` operator. So what do you really need to do? – Luca Angeletti Jul 09 '15 at 21:40
  • @appzYourLife You could tell the same about `>` and `==` for example. They are available for all these types separate, but yet they are "grouped" under corresponding protocols (`Comparable` and `Equatable`). You want a practical application? E.g. writing an aggregate `sum()` method that would work with Collection containing any of numeric types. Without such protocol you have to override such method for each type separately. – Grzegorz D. Jul 09 '15 at 22:19
  • @grzegorzdvipek: Ok I see your point. Thank your for your further explanation. I think the answer by Kametrixom below could solve your problem. – Luca Angeletti Jul 09 '15 at 22:30

1 Answers1

6

There isn't anything in the library for that, I get what you mean. You can do it yourself 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 a + b
}

add(3, b: 4)
Kametrixom
  • 14,673
  • 7
  • 45
  • 62
  • 2
    Yeah, I already wrote the exact same thing :) But, in my opinion, this is one of those things that really should be included in the Standard Library. Especially when Apple likes to call Swift a _Protocol Oriented Programming Language_ ;) – Grzegorz D. Jul 09 '15 at 22:29
  • 1
    Apple listened and there's a protocol for that: 'Numeric'. See https://developer.apple.com/documentation/swift/numeric – Karoly Nyisztor Jul 29 '18 at 08:18
  • @KarolyNyisztor Numeric doesn't support division – Leo Dabus Jun 28 '20 at 04:44