This seems like a very contrived example — most idiomatic Swift code doesn't require using an instance three times in one line. The only API I can think of that does regularly require such a thing is Collection
(e.g. collection[collection.index(after: collection.startIndex)]
), and even then you can declare constants and variables to make the code more clear.
Let's look at some actual real-world examples.
Here's some code from Apple's "Visualizing and Interacting with a Reconstructed Scene" sample project:
// 8. Visualize the center of the face (if any was found) for three seconds.
// It is possible that this is nil, e.g. if there was no face close enough to the tap location.
if let centerOfFace = centerOfFace {
let faceAnchor = AnchorEntity(world: centerOfFace)
faceAnchor.addChild(self.sphere(radius: 0.01, color: classification.color))
self.arView.scene.addAnchor(faceAnchor, removeAfter: 3)
}
Here's some code from Apple's "Compressing and Decompressing Files with Swift Stream Compression" sample project:
let outputFilter = try OutputFilter(operation,
using: algorithm) {(data: Data?) -> Void in
if let data = data {
destinationFileHandle.write(data)
}
}
And here's some code from Apple's "IceCreamBuilder: Building an iMessage Extension" sample project:
// Add the message to the conversation.
conversation.insert(message) { error in
if let error = error {
print(error)
}
}
I think it's evident that this proposal would benefit all three of these real-world examples.
Now, I do acknowledge that you can use shorter identifiers to make your code use fewer characters. But reducing the number of characters in your code isn't really important in the grand scheme of things. From the API Design Guidelines:
Clarity is more important than brevity. Although Swift code can be compact, it is a non-goal to enable the smallest possible code with the fewest characters. Brevity in Swift code, where it occurs, is a side-effect of the strong type system and features that naturally reduce boilerplate.
And that's what this proposal is really about. It's not strictly about brevity, it's about reducing boilerplate.
(Edit: it seems @Ben_Cohen ninja'd me with that last point!)