Should Dictionary support first related methods from Collection conformance?

As long as Dictionary conforms to Collection, it must supply all of Collection's requirements, including first; that's non-negotiable. That said, there is a good argument that Dictionary should not be-a Collection but should instead have-a Collection of key-value pairs… but it may not be the argument you're thinking of.

The usual line is that ordering is fundamental to what a Collection is (which I agree with), and Dictionary is in some sense “unordered” (which I think is flimsy logic at best—Dictionary has an order, because it repeatably yields the same sequence elements on subsequent traversals).

The argument I find more compelling is that Dictionary's conformance to Equatable and Collection are in conflict. As the Equatable documentation says, “equality implies substitutability—any two instances that compare equally can be used interchangeably in any code that depends on their values.” But two equal dictionaries may have different orders, and code that depends on the dictionary's value as a collection will have different behavior depending on which dictionary it is passed.

Based on this conflict it is easy to come up with unimpeachably correct code that operates on equatable collections, but is broken when passed a Dictionary.

We could technically solve this by declaring that a Dictionary's conformance to Collection is not part of its value. Personally I think that creates an unusable mental model, because conformance to Collection exposes a huge amount of API (this is just a fancy way of saying, as you did, that it “creates usage doubts.”)

A small amount of fancy semantic footwork is required even if a Dictionary has-a Collection of key-value pairs: the property representing that Collection has to be declared to be not part of the Dictionary's value. Personally I think that creates a tenable user model because it's just one piece of API that can be documented as such, and—crucially—it is available only in contexts where the programmer knows they have a Dictionary: it's not a problem that is just by virtue of crossing into generic Collection code.

All that said, the last time I brought this issue up with the core team, the verdict was that the harm that would be caused by fixing the problem—forcing substantial amounts of code to change, invalidating lots of extant tutorials on the web, etc.—is worse than the harm caused by the problem itself. And as much as I loathe having demonstrable incorrectness in the standard library design, I find it hard to disagree.

8 Likes