[swift-evolution] Make the first parameter in a function declaration follow the same rules as the others

Haravikk swift-evolution at haravikk.me
Sat Mar 12 12:22:19 CST 2016


> On 11 Mar 2016, at 20:33, Ted F.A. van Gaalen via swift-evolution <swift-evolution at swift.org> wrote:
> 
> When I started using Swift (on the whole a pleasant journey)
> the most confusing thing to me was, and at times still is,  the parameter list,
> 
> 
> I would prefer:
> 
> -uniform for functions(…) AND init(…)
> -every parameter must be used with its name/label. Always, no exceptions.
> -no shortcuts. 
> -allow arbitrary parameter sequence.
> which is possible and very easy to implement when you always have to use names.
> -no trailing commas.

I agree, except for labels always being required; sometimes there’s just nothing to be gained by having a label, such as simple initialisers. Also, well-named functions ought to be clear what the first parameter is, for example:

	func insert(_ element:Element) { … }

No-one’s really going to wonder what a value going into a .insert() method is for. However, requiring the developer to choose to add the underscore (as I did above) to enable this gives a balance between the consistency of having all parameters labelled by default, and being able to omit them where it makes sense to.

In other words, the parameter can be omitted if its label wouldn’t add anything useful to the call-site. There could be an argument that if .insertContentsOf() were restructured then the parameter might become necessary, resulting in the following:

	func insert(element:Element) { … }
	func insert(contentsOf:Sequence) { … }

But I think it’s still good for the API designer to have the option, as some cases just aren’t worth adding a label to (or would be redundant). It’s also handy for internal and private methods/initialisers that don’t need the extra clarity.


More information about the swift-evolution mailing list