[swift-users] Synthesized Equatable Bug?

Jon Shier jon at jonshier.com
Mon Jan 15 16:30:35 CST 2018


Swifters:
	Take this simple Point type:

struct Point: Hashable {
    let x: Int
    let y: Int

    init(_ x: Int, _ y: Int) {
        self.x = x
        self.y = y
    }
}

Using the latest Swift 4.1 snapshot, the Hashable conformance is generated for me. Adding Comparable conformance like this:

extension Point: Comparable {
    static func < (lhs: Point, rhs: Point) -> Bool {
        return (lhs.x < rhs.x  && lhs.y <= rhs.y) || (lhs.x <= rhs.x  && lhs.y < rhs.y)
    }
}

Once this is implemented, (0, 2) <= (1,1) bizarrely evaluates as true, despite both < and == evaluating as false. Manually implementing <= fixes the issue.

static func <= (lhs: Point, rhs: Point) -> Bool {
    return lhs < rhs || lhs == rhs
}

This is pretty straightforward code, so am I missing something here?



Jon


More information about the swift-users mailing list