Closure arguments to map(), find(), etc. should be tagged @noescape
| Originator: | dwineman | ||
| Number: | rdar://19834507 | Date Originated: | 13-Feb-2015 03:56 PM |
| Status: | Closed | Resolved: | Yes |
| Product: | Developer Tools | Product Version: | Xcode 6.3 (6D520o) |
| Classification: | Enhancement | Reproducible: | Always |
Summary: When writing higher-order generic functions, it’s nice to be able to tag the closure argument @noescape, but that precludes passing the closure on to map() or other stdlib HOGFs. Steps to Reproduce: 1. Write a higher-order generic function. For example, map_with_index() which takes a CollectionType and a closure that takes an index and an element of the collection, and works by constructing a sequence of (index, element) pairs and passing it to map(). 2. Notice that calling the new function requires adding “self.” prefixes and [unowned self] at the top, unless you never refer to self, which is OK but kind of a bummer. 3. Read about the new @noescape attribute and get excited because it apparently eliminates all that extra typing AND enables new optimizations, as long as you can guarantee that the closure is only being called or passed as a @noescape parameter, which *should* be the case for map(), right? 4. Add @noescape to your function’s closure argument. Expected Results: New function compiles. Actual Results: Get an error like: “Cannot find an overload for 'map' that accepts an argument list of type '(PairSequence<[C.Index], C>, @noescape (C.Index, C.Generator.Element) -> Bool)’” (Removing the @noescape in this case cures the error.) So @noescape can’t be used unless you never pass your closure argument to any of the generics in the standard library, because their parameters aren’t declared @noescape despite being eligible for it. Which stinks. Regression: @noescape introduced in Swift 1.2. Notes:
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!
Implemented in Xcode 7 beta 5.