Hi!
First, you’re definitely not alone with your opinion on bracing: Swift bracing — Erica Sadun. However, the Swift community settled on 1TBS pretty early on, so you’ll want to use your own linting rules if you’ll be converting others’ work for your own projects.
Have you held the command key while mousing over braces or parentheses yet in Xcode? That highlights the matching symbol. You may find it helpful for visualizing scope; I certainly do when there’s enough nesting going on.
We don’t get into generics in this course, but Catie and I use something like Allman style when generic functions get complex enough, and the opening brace no longer seems to have a most-meaningful connection to what precedes it. I’d be interested to know how you’d format this (an example we use in our own style guide):
/*:
### Multipline `where` clauses
When there is more than one `where` clause, each requires its own line, and is indented below the `where` keyword. The following opening `{` will appear at the beginning of the line after the last requirement.
*/
protocol PAT {
associatedtype Associatedtype
}
func function<
Parameter0: PAT,
Parameter1: PAT
>(
parameter0: Parameter0,
parameter1: Parameter1
)
where
Parameter0.Associatedtype: Protocol,
Parameter1.Associatedtype: Protocol
{
// 🦁
// 🐯
// 🐻
}
As for shorthand syntax in closures, I think your example is a great illustration for a case where positional shorthand names are preferable. Within the closure, you’re quickly giving meaning to the parameters, so they can be given meaningful names. But considering all the closure knows about its parameters, to start, is that they’re integers, and their positions, $0 and $1 are clearer to me than a
and b
, or any alternative. I wish we had the option to use positional syntax in functions too, for cases like this!
It will be interesting to see if you come to a similar opinion after getting over the initial pain of learning Swift closure syntax – I’d love to hear back from you about it (in maybe a year’s time or so?) after you’ve had ample time to experiment and come to find your own standards.
let multiply: (Int, Int) -> Int = {
let (min, max) =
$0 < $1
? ($0, $1)
: ($1, $0)
print(
min, max,
"Arrghh!!! Swift!!!",
getNewString()
)
let product = $0 * $1
print("Result of \($0) * \($1) = \(product)")
return product
}