What is _: in Swift telling me?

Swift

Swift Problem Overview


What does the lone underscore mean in function definitions?

e.g. map(_:)

I understand that when defining functions I can do:

func myFunc(_ string: String) { ... }

Would I then refer to that as myFunc(_:) instead of myFunc(_string:), i.e. purposefully hiding the parameter name?

Swift Solutions


Solution 1 - Swift

The _ is used to define that the parameter is not named

If you have multiple _ it states that you do not need to name the parameters in your function call

func myFunc(name:String, _ age:String){
}

myFunc(“Milo", "I'm a really old wizard")

If you do not use the underscore you would use

myFunc(“Milo”, age: "I'm a really old wizard")

The _ is not necessary for function calls. It is just used to indicate that something does not need to have a name.

In regards to how you would refer to your function, You would not have to pass any name for the function call.
But since you also don’t define a parameter type this seems to me like an invalid example (it at least doesn’t work in Xcode 7 with Swift 2.0)

Edit:
Since Swift 3.0

myFunc(name: “Milo”, age: "I'm a really old wizard")

Should be used

Solution 2 - Swift

Swift needs a convention for saying what the name of a function is, including not only the function name itself (before the parentheses) but also the external names of the parameters. The convention is that the names are followed by colons. So here's a function declaration (in Swift 2.0):

func myFunc(param1 param1:String, param2:String, param3:String) {}

And here is that function's name:

myFunc(param1:param2:param3:)

In real life, however, it is possible (indeed likely) that one or more parameters will not externalize any name. Thus we need a placeholder for that name. The underscore is that placeholder - just as the underscore is the symbol in the declaration suppressing externalization of the name. So, here's another function declaration (in Swift 2.0):

func myFunc2(param1:String, _ param2:String, _ param3:String) {}

And here is that function's name:

myFunc2(_:_:_:)

[The Swift 2.0 spec is important here. In Swift 2.0, the first param name is always not externalized by default, and the other param names are externalized by default. In Swift 1.2 and before, the externalization rules depended on where the declaration appeared, which was unnecessarily inconsistent and confusing.]

Solution 3 - Swift

When referring to a function, in order to disambiguate it is necessary to provide the function name along with the external names of any parameters that it expects.

For example,

func myFunc(myString string: String) { ... }

and

func myFunc(_ string: String) { ... }

represent two different functions, one where an external label is provided for the first String parameter when the function is called and the other where no label is used, as in:

myFunc(myString: "hi")

and

myFunc("hi")

So, in order to identify a function we include the external label for each parameter where ':' indicates that a parameter is to be provided - e.g. yourFunc(arg1:arg2:) will take 2 arguments.

When no external label is used, we place an underscore ('_').

For the 2 functions given above, we would uniquely identify them using:

myFunc(myString:) and myFunc(_:)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser5005945View Question on Stackoverflow
Solution 1 - Swiftmilo526View Answer on Stackoverflow
Solution 2 - SwiftmattView Answer on Stackoverflow
Solution 3 - SwiftfqdnView Answer on Stackoverflow