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-1408] NSArray indexesOfObjectsPassingTest cannot be called in Swift 3 #44017

Open
mattneub opened this issue May 4, 2016 · 4 comments
Open
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@mattneub
Copy link

mattneub commented May 4, 2016

Previous ID SR-1408
Radar rdar://problem/25607552
Original Reporter @mattneub
Type Bug
Environment

Xcode 7.3.1 and any recent Swift 3 toolchain

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, ClangImporter
Assignee None
Priority Medium

md5: 1d39ecdeb1ffe00f63e421b47bc0eb59

is duplicated by:

  • SR-1857 Using PHFetchResult.enumerateObjects() with trailing closure syntax causes compiler error
  • SR-2425 NsMutableIndexSet Enumerate Issue

relates to:

  • SR-2838 renamification creates ambiguity
  • SR-3550 Compound name syntax for functions with no params

Issue Description:

The following code doesn't compile in any recent toolchain, due to issue with renamification and the rules for Swift default parameters:

            let pep = ["Manny", "Moe", "Jack"] as NSArray
            let ixs : NSIndexSet = pep.indexesOfObjects(passingTest: {
                (obj:AnyObject, ix:Int, stop:UnsafeMutablePointer<ObjCBool>) -> Bool in
                return true
            })

Basically it is now unnecessarily difficult to call NSArray indexesOfObjects. The reason is that Swift sees the passingTest: parameter as ambiguous. Now, you might say: huh? Ambiguous with what? Well, it's rather subtle. Here's the detailed complaint from the compiler:

Foundation.NSArray:77:17: note: found this candidate

    public func indexesOfObjects(passingTest predicate: (AnyObject, Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> NSIndexSet

Foundation.NSArray:80:17: note: found this candidate

    public func indexesOfObjects(_ opts: NSEnumerationOptions = [], passingTest predicate: (AnyObject, Int, UnsafeMutablePointer<ObjCBool>) -> Bool) -> NSIndexSet

The nameless parameter in the second method has a default value, and both methods have a `passingTest` parameter, so in the absence of further information, our code could now be calling either of them.

This analysis suggests (rightly) that we can worm our way out of the difficulty by saying:

            let ixs : NSIndexSet = pep.indexesOfObjects([], passingTest: {
                (obj:AnyObject, ix:Int, stop:UnsafeMutablePointer<ObjCBool>) -> Bool in
                return true
            })

Yecch! What is the point of having an optional parameter (a parameter with a default value) if you have to supply it anyway? How did we get ourselves into this mess in the first place?

The answer is that Objective-C has two perfectly separate methods:

    indexesOfObjectsWithOptions:...

and

    indexesOfObjectsPassingTest:...

A moment's thought will reveal that Swift renamification has conflated these two methods into one. It is therefore impossible to call indexesOfObjectsPassingTest — it is effectively stricken from the field. Only indexesOfObjectsWithOptions can be called.

This could easily be resolved by giving the options parameter an explicit label. We would then have indexesOfObjects(options:... ) and indexesOfObjects(passingTest:... ) and all would be well.

@belkadan
Copy link
Contributor

belkadan commented May 4, 2016

The explicit label wouldn't help; it's still defaulted.

@mattneub
Copy link
Author

mattneub commented May 4, 2016

I see now that I also reported this, rather less perceptively, as rdar://problem/25770004 — it has taken me a while to understand why Swift thinks there's an ambiguity

@belkadan
Copy link
Contributor

belkadan commented May 4, 2016

Thanks, Matt. I've consolidated it with the earlier Radar mentioned above.

@swift-ci
Copy link
Collaborator

Comment by Stephan (JIRA)

I also had the same issue. You can see my specific case here:
http://stackoverflow.com/questions/39569250/after-swift-3-conversion-i-cant-get-rid-of-error-ambiguous-use-of-indexofob/39572461#39572461

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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