There was an interesting question on StackOverflow this morning: Why doesn’t NSOrderedSet inherit from NSSet? It’s interesting because the reason is so easy to miss. I thought it would make a good blog post because it turned out to be a nice, real-life example of the Liskov substitution principle (herein abbreviated to LSP).
He points to the
-mutableCopy method, but I think there’s a problem with the
-isEqual: method even when considering only immutable sets. An ordered set wants to include order in its definition of equality, while a regular set cares only about membership. Equality can’t be implemented in a way that’s symmetric and transitive. Thus, it makes sense for them not to be subtypes.