It’s designed to allow functions to have “two faces.” One face is the one presented to the caller, and the other to the function, itself.
The idea is to maximize grokability. The function’s purpose and argument list is clear to the caller.
It’s an attempt at that old “philosophers’ stone” of “self-documentation.”
Almost all the code I encounter has little to no method interface documentation. This is supposed to address that.
My experience is that many people circumvent it by using the “wildcard” (_).
I use it to add “in” prefixes to my parameters, like so:
func churn(butter inButter: String) {...}
The “in” prefix means nothing, outside the function context, but is an indicator that it is a function parameter, inside the function.
However, outside the function, the parameter name makes it clear that what is to be churned, is butter, as this is the published signature:
func churn(butter: String)
It also allows us to use parameters that are published with the names of class properties, without name collisions.
For example:
init(butter inButter: String) { butter = inButter }
Where using just “butter” would mean we need to do this:
init(butter: String) { self.butter = butter }
Small stuff, but Swift has a lot of these types of considerations.
But what is really cool about function parameters, is that we can “skip” optional parameters, like so:
func add(numberOne inFirst: Int = 0, numberTwo inSecond: Int = 0, numberThree inThird: Int = 0, to inTarget: Int) {
return inFirst + inSecond + inThird + inTarget
}
We can call it like so:
let result = add(numberThree: 4, to: 5)
I write about that sort of thing, here: https://littlegreenviper.com/miscellany/swiftwater/swift_fun...
It’s an enormously flexible language, and can be used to write almost inscrutable code, if we choose.
However, it can also be used to write extremely readable code.
I think it’s a sisyphean exercise, trying to get programmers to write readable code, but we keep trying.
Like I said, Swift is a bit like C, where it can be used to write obfuscated junk, if the programmers want.
My experience is that “idiomatic Swift,” as prescribed by many authorities, is starting to look like that.
I’m not exactly sure what you mean by “escape an opening paren.”
Do you mean inside of strings? That’s a direct inline shorthand that is similar to `...` in other languages. Totally optional.
For example: print(“There are \(numberOfSheep) sheep.”) is direct, as opposed to print(“There are “ + numberOfSheep + “ sheep.”), or print(“There are “, numberOfSheep, “ sheep”).