swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.6k stars 10.37k forks source link

[SR-94] Allow closure parameters to throw for Objective-C #42716

Open zwaldowski opened 8 years ago

zwaldowski commented 8 years ago
Previous ID SR-94
Radar rdar://problem/26742476
Original Reporter @zwaldowski
Type Improvement
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 1 | |Component/s | Compiler | |Labels | Improvement, ClangImporter, PrintAsObjC | |Assignee | None | |Priority | Medium | md5: 290e334cda7650c06f329c05489b169f

relates to:

Issue Description:

Currently, rethrowing functions cannot be exported to Objective-C, even if all types are Obj-C compatible. Furthermore, a throwing closure type cannot be marked as @convention(block), citing that the type is not representable.

This limits the ability to propagate errors when wanting to export for Objective-C, or implementing the same in Objective-C for locking, queueing, etc. One might expect the block type to get exported with the same (…, NSError **) -> BOOL expansion functions themselves are. Consider the following signature, inspired by NSDocument:

extension SomeDocument {
    func performSynchronousAccess(body: NSURL throws -> Void) rethrows
}

It should be imported into Objective-C approximately (there are some weirdnesses with the parameter names) as:

@interface SomeDocument (Swift)
- (void)performSynchronousAccessAndReturnError:(NSError **)error block:(void(^)(NSURL *, NSError **))body;
@end

The same desire applies in reverse; I should be able to implement -performSynchronousAccessAndReturnError:block: from Objective-C. One might expect the full gamut of the swift_error attribute to apply to block and function parameters, if not blocks and functions as a whole.

belkadan commented 8 years ago

Hm. I don't see anything inherently wrong here, but there's not really precedent for this in Cocoa / Cocoa Touch.

belkadan commented 8 years ago

I stand corrected: the Photos library uses something similar.