Wednesday, August 8, 2012 [Tweets] [Favorites]

Why NSOrderedSet Doesn’t Inherit From NSSet

Tom Dalling:

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.

1 Comment

[...] Michael Tsai correctly points out another LSP violation. If NSOrderedSet inherits from NSSet, then isEqual: can not correctly compare an ordered set to an [...]

Stay up-to-date by subscribing to the Comments RSS Feed for this post.

Leave a Comment