Variable names `count` leads to incorrect error when Swift.count method is used inside Array.filter
| Originator: | javier.api | ||
| Number: | rdar://20961110 | Date Originated: | 14-May-2015 01:23 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode 6.4 (6E23), Swift 1.2 |
| Classification: | UI/Usability | Reproducible: | Always |
Summary:
On a long playground, it was hard to figure out what was wrong with my use of someArray.filter { … }.map { … }
Steps to Reproduce:
Put the following code in a playground:
let count = 3
let array = ["a", "b", "abcdefg"]
array.filter { count($0) > 1 }.map { $0.uppercaseString }
Expected Results:
Either code fails to compile with an error pointing at `count` not being a function inside the filter closure, or the code compiles because it can infer that we’re referring to the Swift.count function, and not to the count variable defined above.
Actual Results:
The code fails to compile with an error that points at the wrong thing:
Playground execution failed: CountWrongErrorTestCase.playground:4:32: error: cannot invoke 'map' with an argument list of type '((_) -> _)'
array.filter { count($0) > 1 }.map { $0.uppercaseString }
^
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!
count variable and global count() function
this counts for all count declarations in the same scope. I wanted to call count() and had a count var (like you) declared in the same scope (or above). You could not call count(). The compile gives an error pointing to the wrong thing.