Any Array with an element type that conforms to Equatable can be checked for equality like eg:
let a = ["one", "two"]
let b = ["one", "foo"]
print(a == b) // false
This is not because Arrays in general, or Arrays with Equatable element types in particular, conform to Equatable (neither does). But the standard library has this:
/// Returns true if these arrays contain the same elements.
func ==<T : Equatable>(lhs: [T], rhs: [T]) -> Bool
I can understand why all possible Array<T> types (for any T) can't conform to Equatable (because it simply wouldn't be possible to check the element-pairs of any two such arrays for equality (since their element type wouldn't have to be Equatable)).
But what about all Array<T: Equatable> types? I can't see any good reason for why those shouldn't / couldn't conform to Equatable. Please let me know if you can.
Details:
Here's what the Equatable protocol requires:
protocol Equatable {
func ==(lhs: Self, rhs: Self) -> Bool
}
That requirement is, as far as i can see, already satisfied by any and all Array<T: Equatable> types (see the above == function from the standard library), the only thing missing is the declaration of that conformance, something like
extension Array where T: Equatable: Equatable {}
or
extension Array: Equatable where T: Equatable {}
none of which compiles (which seems reasonable looking at the syntax alone, but I can't come up with any other way to express that).
For reference, here's an alternative implementation of the above ==:
public func ==<T: CollectionType where T.Generator.Element: Equatable>(lhs: T, rhs: T) -> Bool {
return lhs.count == rhs.count && (zip(lhs, rhs).contains { $0.0 != $0.1 }) == false
}
So, I must be missing something here, probably one of these:
1. An existing way to declare that all Array<T: Equatable> types conform to Equatables (but if so, why hasn't that been done in the std lib?).
2. A possible future way to declare that all Array<T: Equatable> types conform to Equatable.
3. A good reason for why this isn't and never will be possible.
Which one is it?