Comparable’s Inheritance

Comparable actually inherits from Equatable. You may be able to guess what the implication of this inheritance is. In order to conform to the Comparable protocol, you must also conform to the Equatable protocol by supplying an implementation of the == operator. This relationship also means that a type does not have to explicitly declare conformance to Equatable if it declares conformance to Comparable. Remove the explicit declaration of conformance to Equatable from your Point struct.

Listing 25.10  Removing the unnecessary conformance declaration

struct Point: Equatable, Comparable { let x: Int let y: Int } func ==(lhs: Point, rhs: Point) -> Bool { return (lhs.x == rhs.x) && (lhs.y == rhs.y) } func <(lhs: ...

Get Swift Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.