-3

I'm new to Swift and I want to write a generic max function which compares the two parameter and returns the larger one, for basic types like Int, Double, etc.

func max<T>(_ num1:T, _ num2:T) -> T {
    return (num1 > num2) ? num1 : num2;
}

But I found this does't work, reported that Binary operation '>' cannot be applied to two 'T' operand.

I saw an example about generic add function Here

protocol Summable { static func +(lhs: Self, rhs: Self) -> Self }
extension Int: Summable {}
extension Double: Summable {}

func add<T: Summable>(x: T, y: T) -> T {
  return x + y
}

So I think I should have a protocol for my max function, too. So this is my attempt:

protocol Comparable {
    static func >(lhs: Self, rhs: Self) -> Self
}
extension Int:Comparable {}
extension Double:Comparable {}

But this doesn't work. I know there is a provided Comparable protocol from Swift, but I want to try it myself. Could you please help?

keanehui
  • 185
  • 2
  • 13
  • 3
    Why don't you use the Swift max func? let maxNum = max(firstNum, SecondNum) – DungeonDev Jun 18 '20 at 09:12
  • @DungeonDev Yeah that's an option, but I prefer to understand the use of `protocol` for future use. I read some documents from Apple and I'm still confused. – keanehui Jun 18 '20 at 09:15
  • 3
    I don't see any added value in duplicating the already existing `Comparable` protocol, [since its implementation is open source](https://github.com/apple/swift/blob/da61cc8cdf7aa2bfb3ab03200c52c4d371dc6751/stdlib/public/core/Comparable.swift), same for the [max function](https://github.com/apple/swift/blob/da61cc8cdf7aa2bfb3ab03200c52c4d371dc6751/stdlib/public/core/Algorithm.swift#L54). This means that even as a learning opportunity, this Q&A holds no value whatsoever for future readers. – Dávid Pásztor Jun 18 '20 at 09:38

2 Answers2

1
protocol TempComparable {
    static func >(lhs:Self, rhs:Self) -> Bool;
}

func max<T:TempComparable>(_ num1:T, _ num2:T) -> T {
    return (num1 > num2) ? num1 : num2;
}
keanehui
  • 185
  • 2
  • 13
  • 3
    I don't see any added value in duplicating the already existing `Comparable` protocol, [since its implementation is open source](https://github.com/apple/swift/blob/da61cc8cdf7aa2bfb3ab03200c52c4d371dc6751/stdlib/public/core/Comparable.swift), same for the [max function](https://github.com/apple/swift/blob/da61cc8cdf7aa2bfb3ab03200c52c4d371dc6751/stdlib/public/core/Algorithm.swift#L54). This means that even as a learning opportunity, this Q&A holds no value whatsoever for future readers. – Dávid Pásztor Jun 18 '20 at 09:37
0

What you need is to create your protocol as the sub-protocol of Comparable and provide a unique name instead of naming it same as an existing Type which causes confusion. And implement the protocol requirements in the extension of your protocol and conform the required types to the protocol. Here's how:

protocol CustomComparable: Comparable {
    static func > (lhs: Self, rhs: Self) -> Self
}
extension CustomComparable {
    static func > (lhs: Self, rhs: Self) -> Self {
        lhs > rhs ? lhs : rhs
    }
}
extension Int: CustomComparable {}
extension Double: CustomComparable {}
Frankenstein
  • 15,732
  • 4
  • 22
  • 47