Type 'TestClass' does not conform to protocol 'Equatable'

func test() {

class TestClass: Equatable {
    var i: Int
    
    init(i: Int) {
        self.i = i
    }
    
    static func ==(lhs: TestClass, rhs: TestClass) -> Bool {
        return lhs.i == rhs.i
    }
}

}

Can't understand, why does this code produce compile-time error? What am i doing wrong?

@nikitahils, to format your code snippet, surround it with triple backticks (```) on the lines above and below.

I also observe the error you describe, using Swift 5.0.1 in an Xcode 10.3 playground as well as in the REPL.

Furthermore, I observe the following:

• Placing the declaration at the top level (outside a func) lets it compile.
• Removing the protocol conformance “: Equatable” lets it compile.
• Changing class to struct lets it compile.

This appears to be a bug, and I would recommend you file it at bugs.swift.org

Wow, I didn't know you could nest types within functions!

You can also let them leak out, which is interesting:

func f() -> Any {
	struct S { let i: Int }
	return S(i: 5)
}

f() // => S(i: 5)

This is a known bug [SR-3092] Function-level nested types cannot conform to Equatable · Issue #45682 · apple/swift · GitHub

1 Like