Skip to content

Swift v2 Error Handling #152

@adonoho

Description

@adonoho

Gentlefolk,

The Swift v2 try-catch mechanism maps onto Objective-C quite well. Except when there is a shadowing problem.

For example:

- (BOOL)pinWithName:(NSString *)name error:(NSError **)error;

which automatically maps into Swift v2 in [redacted] v7b6 headers as:

public func pinWithName(name: String, error: ()) throws

It should probably map as:

public func pinWithName(name: String) throws

The Objective-C method Is also commonly paired with this shortened form:

- (BOOL)pinWithName:(NSString *)name;

Which maps as:

public func pinWithName(name: String) -> Bool

This short method, I suspect, just calls the main one with a NULL for the NSError**. The signature for the short method is remarkably similar to the Swift throwing method. If I leave the short method in my Objective-C header, then errors ensue:

do { try location.pinWithName(kLocationPin)
    updatePinnedLocations() // Add the locations to the user in the cloud.
}
catch let e as NSError { log.error("Local location save error: \(e)") }

Results in the following compile time warning.

warning: no calls to throwing functions occur within 'try' expression
       do { try location.pinWithName(kLocationPin)
            ^

Obviously, the compiler has chosen the wrong signature. In my own code, where I express a similar pattern, I comment out the short method, then the app compiles just fine.

What is the right way to fix the public API? In most things Swift, the answer would be to get rid of the short Objective-C method.

Or is there a workaround to the above problem? In my own code, I have tried using the NS_SWIFT_NOTHROWS annotation on the long method signature. It allows me to retain my Swift v1.2 error handling code mostly without change.

What would be the right solution for the Parse API?

Anon,
Andrew

Metadata

Metadata

Assignees

No one assigned

    Labels

    type:featureNew feature or improvement of existing feature

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions