[swift-evolution] [Idea] Omit argument labels from the call site when the argument is the same as the label

Alex Hoppen alex at ateamer.de
Sun Feb 7 14:41:20 CST 2016


I don’t think it is possible to implement this without major changes to the way function overloads are selected.

Consider the following example with where there are multiple functions that can print a Double (yes, the external arguments could be put in the function’s name, but I hope it shows my point):

func myPrint(number: Double) { }
func myPrint(withTwoDecimals number: Double) { }
func myPrint(withThreeDecimals number: Double) { }

Currently all three of these functions have different signatures. If your syntax would be implemented in Swift, then

myPrint(withTwoDecimals)

would be ambiguous. 

Defaulting to the function without external parameter names in case of ambiguity isn’t an option because then the call would change its behaviour the moment a library adds myPrint(number: Double) in addition to the other functions. This is definitely not desirable! The same applies for defaulting to the function with the external parameter name.

I don’t like leaving the function call ambiguous because then the only way to call myPrint(number: Double) would be to change the name of the variable. The name of any variable should by private and therefore not have any effect on how the function is called. (Also think about functions where there is basically only one proper way to name a variable, like x for coordinates. You would have to rename the variable which leads to less descriptive variable names).

Thus, the only way to resolve this issue IMO would be to say that the signatures myPrint(number: Double) and myPrint(withTwoDecimals number: Double) are identical. Then, however, the signatures of myPrint(withTwoDecimals number: Double) and myPrint(withThreeDecimals number: Double) would need to be equivalent as well, which seriously limits the way functions can be overloaded in Swift. I don’t think that the improved syntax is worth paying with the this expressiveness of function overloads.

- Alex

> On 07 Feb 2016, at 20:45, Kenny Leung via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Hi All.
> 
> I’m just throwing this out there.
> 
> often when I write Objective-C, my method calls end up looking like this:
> 
> [you doYourThingWithThis:this that:that these:these those:those other:other];
> 
> This reads poorly and requires extra typing. It would be nice if the compiler just assumed that when no label is given, the label is the same as the argument name. The code would be just as understandable (more) than if the argument labels were actually there.
> 
> In Swift, this:
> 
> you.doYourThingWithThis(this, that:that, these:these, those:those, other:other)
> 
> becomes
> 
> you.doYourThingWithThis(this, that, these, those, other)
> 
> However, if you are the type that likes to name your variables a,b,c,d, no harm done, since then labels would be required, and it would still read OK
> 
> you.doYourThingWithThis(a, that:b, these:c, those:d, other:e)
> 
> -Kenny
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160207/a3e3675c/attachment.html>


More information about the swift-evolution mailing list