Overzealous warning about swift optional argument naming
| Originator: | nicklockwood81 | ||
| Number: | rdar://18983844 | Date Originated: | 14/11/2014 |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | |
| Classification: | Reproducible: |
Summary:
In Swift, the convention for naming method arguments is that the first argument name is part of the method name, e.g.
func doSomethingWithFoo(foo: Int) { }
Such that the method will be called like this:
self.doSomethingWithFoo(5)
For that reason, the compiler warns you not to name your first parameter using "with", so declaring your method like this raises a warning:
func doSomething(withFoo: Int) { }
Because then it won't be clear what you mean when you call
self.doSomething(5)
This is all fine so far. The problem is that if the first parameter is optional, then the calling convention is different. Suppose that I declare my method like this:
func doSomething(withFoo foo: Int = 5) { }
I can call it like this:
self.doSomething() // foo defaults to 5
Or like this:
self.doSomething(withFoo: 7) // foo is 7
The problem is that the compiler doesn't want me to name my first parameter with a "with" prefix, and so still complains, even though it makes the code clearer in this case.
Steps to Reproduce:
Run sample project. Observe spurious warning.
Expected Results:
Compiler should recognise that use of withX for first param is appropriate if parameter is optional
Actual Results:
Compiler complains that "with is implied for first parameter"
Version:
6.1
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!