Swift 3 New Features by Keith Elliott

Swift 3 New Features by Keith Elliott

Author:Keith Elliott
Language: eng
Format: epub
Publisher: Packt Publishing


Note

You can read more about the proposal at https://github.com/apple/swift-evolution/blob/master/proposals/0066-standardize-function-type-syntax.md

Enforcing the order of defaulted parameters [SE-0060]

Order matters when you call a function in Swift, but there is one exception to this rule for functions that contain parameters with default arguments. Under this edge case, you can call this type of function using only a portion of the argument names. Let's look at some examples of how you can call a function that contains default parameters.

In Swift 2:

func shifty(arg1: String = "", arg2: String = "", arg3: Int = 1){}

The first way to call the shifty() function is to use all of the default parameters, meaning we don't pass anything at the call site. This is valid in Swift 2 and is generally expected behavior. See below for an example using just the function with all default parameter values:

shifty()

Another way to call the shifty() would be to omit the arguments we don't care about and only pass in the ones we do. We could pass in just one argument such as arg2 or arg3 and our function would continue to work. In the following example, we demonstrate calling our shifty function while omitting some of the parameters:

shifty(arg2: "") shifty(arg3: 3)

Finally, we could call the function with multiple arguments. See below for example usage of the shifty function with multiple arguments:

shifty(arg2: "", arg3: 3) shifty("", arg3: 3) shifty(arg2: "", arg3: 4)

Allowing this behavior is actually a bit confusing to developers and goes against the strict ordering that is enforced throughout the rest of the language. Swift 3 removes this behavior and forces you to maintain the parameter ordering when using default parameters. Let's see how our shifty() function works now in Swift 3.

In Swift 3:

func shifty(arg1: String = "arg1", arg2: String = "arg2", arg3: Int = 0){}

There is no change to how we call a function using all defaulted arguments:

shifty()

When we call a function with just one argument, we have to use the argument label for our parameter. The other difference is that we can not just call the arguments in any order we choose. We can omit defaulted parameters but we can not call them in any order we choose. Let's examine some of the ways we can call our shifty() function in Swift 3.

shifty(arg1: "") // valid shifty(arg2: "") // valid shifty(arg3: 3) //valid shifty(arg3: 3, arg1: "test") //invalid!

If you think about this for a while, I bet you could see why the Swift team made this change. We sacrifice a shorter syntax for enhanced readability.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.