ios – Array of protocols utilizing kind erasure can not conform to Hashable or Equatable protocols

0
42


I am operating into a difficulty whereby the sample I’m making an attempt to implement doesn’t play properly with SwiftUI statefulness and refuses to replace structs that conform to my TestProtocol when they’re in an array.

I’m chalking this as much as my elementary misunderstanding so would respect any steering on the matter.

Here’s a sanitized code snippet

protocol TestProtocol: Identifiable, Hashable, Equatable  {
    var id: any Identifiable { get }
}

struct TestStruct: Identifiable, Hashable, Equatable {
    let id: UUID = UUID()

    let testArray: [any TestProtocol]

    public func hash(into hasher: inout Hasher) {
        hasher.mix(id)
        hasher.mix(testArray) /// ERROR: Sort 'any TestProtocol' can not conform to 'Hashable'
    }

    static func == (lhs: Self, rhs: Self) -> Bool {
        lhs.id == rhs.id && lhs.testArray == rhs.testArray /// ERROR: Sort 'any TestProtocol' can not conform to 'Equatable'
    }
}

class TestObservable: ObservableObject {
    static let shared = TestObservable()

    @Printed var filters: [TestStruct]

    init() {
        self.filters = [
            TestStruct(
                testArray: [
                    ...
                ]
            )
        ]
    }
}