Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-5494] can't compare multidimensional arrays for equality in Swift 4 #48066

Closed
mattneub opened this issue Jul 18, 2017 · 8 comments
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@mattneub
Copy link

Previous ID SR-5494
Radar None
Original Reporter @mattneub
Type Bug
Status Resolved
Resolution Duplicate
Environment

Xcode Version 9.0 beta 3 (9M174d)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: b7d9f98045b644a724915626edb9d54d

duplicates:

  • SR-2284 Cannot invoke 'XCTAssertEqual' with an argument list of type '([Array], [[Int]])'

Issue Description:

The following doesn't compile:

let arr1 = [[0]]
let arr2 = [[0]]
let ok = arr1 == arr2 // binary operator '==' cannot be applied to two '[[Int]]' operands

This used to work, so I have to wonder whether this is a new Swift 4 bug.

@mattneub
Copy link
Author

Okay, it doesn't work in Swift 3 either. So now I'm not sure when it stopped working. But it did once work, because I've got old code that used it and that has now stopped compiling. So I'm not actually clear on the history, sorry.

@mattneub
Copy link
Author

See also https://bugs.swift.org/browse/SR-2284

@mattneub
Copy link
Author

Cannot use flatMap as a workaround because you can get false positives:

let arr1 = [[0],[1,2]]
let arr2 = [[0,1],[2]]
let ok = arr1.flatMap{$0} == arr2.flatMap{$0} // true, but that's not the "right" answer

@belkadan
Copy link
Contributor

It has never worked properly. If you have Foundation imported it used to convert to NSArray and compare those, and it no longer does that.

@mattneub
Copy link
Author

I don't quite see why that's a resolution. It's an array. Arrays are compared for equality by comparing their elements in order for equality. The elements are arrays, so they can be compared for equality. Why doesn't the language just do this? What am I expected to do here — implement my own completely unnecessary `==` override?

@belkadan
Copy link
Contributor

Array's Element type might not be Equatable, so Array itself can't be declared Equatable until we have conditional conformances. But this is one of the first ones we want to add when we get that!

@jckarter
Copy link
Member

It will "just work" when we get conditional conformances. In the meantime, you could use the `elementsEqual` method, or cast the arrays explicitly `as NSArray` to get back to the Swift 2 behavior.

@mattneub
Copy link
Author

@jckarter Thanks as always; `elementsEqual` is a great suggestion. I guess I don't quite understand why that isn't exactly what `==` falls back on by default; but at least it does precisely what's needed.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

3 participants