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
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.
Deep Learning with Python by François Chollet(12579)
Hello! Python by Anthony Briggs(9917)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9796)
The Mikado Method by Ola Ellnestam Daniel Brolund(9780)
Dependency Injection in .NET by Mark Seemann(9340)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8303)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7698)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7100)
Microservices with Go by Alexander Shuiskov(6870)
Practical Design Patterns for Java Developers by Miroslav Wengner(6783)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6725)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6419)
Angular Projects - Third Edition by Aristeidis Bampakos(6140)
The Art of Crafting User Stories by The Art of Crafting User Stories(5660)
NetSuite for Consultants - Second Edition by Peter Ries(5595)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5404)
Kotlin in Action by Dmitry Jemerov(5067)
