All Swift method parameters should be keyworded
| Originator: | brent | ||
| Number: | rdar://17284787 | Date Originated: | 12-Jun-2014 03:40 AM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode6-Beta (6A215l) |
| Classification: | New Feature | Reproducible: | Always |
Summary:
Every parameter to a Swift method should have a keyword by default.
Objective-C got to be as readable as it is because every selector reads well. Part of “reading well” is that every parameter is labeled. Swift apparently has trouble "seeing" this structure, so it does not attempt to label the first parameter of Objective-C methods; instead it blindly translates them. For instance, the selector -postNotificationName:object:userInfo: is translated as postNotificationName(_:object:userInfo:), when it should clearly be postNotification(name:object:userInfo:).
I think that an appropriate set of heuristics could nearly always split Objective-C names correctly. But even if you can't, Swift should not make the problem worse by encouraging native Swift methods to use the same broken syntax.
(I actually think function parameters should be keyworded too, but that’s a different radar.)
Steps to Reproduce:
1. Create a Swift file.
2. Define a method: class X { class func foo(x: Int, y: Int) { /* do nothing */ } }
3. Write a call to it: X.foo(x: 1, y: 2)
Expected Results:
The call works by default.
Actual Results:
The call does not work because some Swift parameters are not labeled.
Notes:
I noticed that the swift-ide-test tool has some heuristics to split Objective-C selectors. I think those heuristics could be improved, perhaps enough to fully activate them, by examining not only the selector, but not only the first parameter name. Here’s what I mean:
Compare the keyword “postNotificationName” and the parameter “aName” case-insensitively. The two have a common suffix, “Name”, and the first letter of this suffix in the selector is capitalized. Therefore, “postNotification” is the method name and “name” is the keyword.
You may need to ignore common suffixes like “Ptr” on the parameter name, and there may be cases where it doesn’t work. But combined with your current heuristics, like splitting on words like “With” or “Using”, you can probably get nearly all Cocoa methods right.
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!