[swift-evolution] Immediately-called closures should be considered @noescape

Brent Royal-Gordon brent at architechies.com
Tue Dec 15 18:17:43 CST 2015


>> Here’s a little annoyance I ran across just now:
>> 
>>           let sub: CKSubscription = {
>>               if let ID = self.ID {
>>                   return CKSubscription(recordType: typeName, predicate: predicate, subscriptionID: ID, options: allMutations)
>>               }
>>               else {
>>                   return CKSubscription(recordType: typeName, predicate: predicate, options: allMutations)
>>               }
>>           }()
>> 
>> This closure obviously never leaves the enclosing function, but Swift doesn’t treat it as if it had been passed to a @noescape function, so I have to say “self.ID” instead of “ID” in the condition. That’s a bit irritating, and I’d like to see it improved.
> 
> Would the following be acceptable, or too dense?
> 
> let sub = ID.map { id in
>  CKSubscription(recordType: typeName, predicate: predicate, subscriptionID: id, options: allMutations)
> } ?? CKSubscription(recordType: typeName, predicate: predicate, options: allMutations)

It’s clever, but a little too clever even for my code.

(Really, the actual problem in this piece of code is that you can’t pass a nil subscriptionID to CKSubscription’s initializer even though there’s a parallel initializer which omits that parameter. Some people just want to watch the world burn, I guess.)

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list