Why can't existential types be compared?

This is one of many reasons you should still go through as? AnyHashable first when implementing a heterogeneous equality operator. AnyHashable handles subclassing, bridging, numeric representation differences, and other dynamic cases that should usually be considered equivalent in dynamic situations. (You can still do the as? any Equatable and existential opening thing as a second pass if you care about types that aren't Hashable, since all of the types that have special handling are Hashable.) Note that AnyHashable, and Swift's dynamic casting in general, consider nils of different Optional types to be equivalent, and I think that's the right thing for a heterogeneous equality operator to do for consistency.

4 Likes