Skip to content

[Concurrency] Don't lose name information from completion-handler arguments #34394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

DougGregor
Copy link
Member

When a completion handler parameter has a selector piece that ends with
"WithCompletion(Handler)", prepend the text before that suffix to the
base name or previous argument label, as appropriate. This ensures that
we don't lose information from the name, particularly with delegate names.

…uments.

When a completion handler parameter has a selector piece that ends with
"WithCompletion(Handler)", prepend the text before that suffix to the
base name or previous argument label, as appropriate. This ensures that
we don't lose information from the name, particularly with delegate names.
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@harlanhaskins
Copy link
Contributor

While you're here, would it make sense to add WithCompletionBlock as well?

@DougGregor
Copy link
Member Author

While you're here, would it make sense to add WithCompletionBlock as well?

FWIW, it doesn't show up in the iOS or macOS SDKs at all, and a general web search finds relatively few uses.

@DougGregor DougGregor merged commit b094758 into swiftlang:main Oct 22, 2020
@DougGregor DougGregor deleted the concurrency-objc-name-adjustments branch October 22, 2020 23:38
@@ -16,6 +16,8 @@

-(void)doSomethingConflicted:(NSString *)operation completionHandler:(void (^)(NSInteger))handler;
-(NSInteger)doSomethingConflicted:(NSString *)operation;
-(void)server:(NSString *)name restartWithCompletionHandler:(void (^)(void))block;
-(void)server:(NSString *)name atPriority:(double)priority restartWithCompletionHandler:(void (^)(void))block;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: I'm not sure what real-life APIs you're looking at, but at least rationalizing based on this example, it may make the most sense to translate these APIs by prepending the information to the base name at all times: await is really taking the place of the completion handler, and it lives on the opposite side of the expression as compared to a trailing closure.

   server:atPriority:restartWithCompletionHandler:
//                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ trailing closures are ergonomic

   await server.server("localhost", atPriorityRestart: 0.8)
// ^^^^^                                      ^^^^^^^ await ... wait for it ... restart

If the Objective-C importer were to translate these names by first logically shuffling this information to the front, then it seems to me like it'd be a consistently better result:

// Original
thing:frobnicateWithCompletionHandler:

// Rearrange to pull name information next to `await` use site
frobnicateWithCompletionHandler:thing:

// Eliminate "WithCompletionHandler"
frobnicateThing:

When the design for async/await is eventually shared, I hope you'll devote some text to translation from legacy APIs for discussion and possible enhancement, just like the review process that the original Objective-C-to-Swift translation went through.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants