Type 'CGPoint' does not conform to protocol 'Hashable'

When I try to make CGPoint Hashable I get an error:

extension CGPoint: Hashable {} // Type 'CGPoint' does not conform to protocol 'Hashable'

However if I create my own duplicate struct it works:

struct CGPoint2 {
    var x: CGFloat
    var y: CGFloat
}

extension CGPoint2: Hashable {}

What is going on here?

"Hashable" conformance can only be synthesized in case it's declared in the same file with the type.
Since the file you declare Hashable conformance for CGPoint in is different from the file where CGPoint is declared, conformance cannot be synthesized.

6 Likes

Ah, I didn't know about this restriction, thanks!

I did try moving the extension of CGPoint2 to a different file and in that case I get a more useful error:

Implementation of 'Hashable' cannot be automatically synthesized in an extension in a different file to the type

2 Likes

It's generally worth reporting unhelpful error messages on bugs.swift.org (there already is a report for this particular issue, but please try to report bad error messages when you encounter them; an unclear error message is always a bug).

11 Likes
Terms of Service

Privacy Policy

Cookie Policy