[swift-evolution] Parameter names in closures in Swift 3 are no longer possible

Andrew Hart andrew at projectdent.com
Sat Oct 8 05:47:06 CDT 2016


I’ve been a little dismayed to see that closures in Swift 3 no longer have
parameter names. As an example, in Swift 2, a function with a completion
block may look like this:

func sendMessage(completion: (success: Bool, recipientID: String?,
senderID: String?) -> Void) {
    //Oh no it failed
    completion(success: false, recipientID: nil, senderID: nil)
}

Now, in Swift 3, it looks like this:

func sendMessage(completion: @escaping (_ success: String, _ recipientID:
String?, _ senderID: String?) -> Void {
    //Oh no it failed
    completion(false, nil, nil)
}

So now all parameter names, if you wish to include them, must be preceded
by a _. Removing the _, it forces you to put it back. Or putting the
parameter name twice, it forces you to replace the first occurrence with a
_.

To me, one of the great advantages of Swift over a language like Python or
Ruby is its self-documenting nature. If I’m calling a function, I don’t
need to look elsewhere for a reference to the parameters I’m using. When
reading back over my code, I don’t have to look elsewhere to check whether
I used the correct parameters, and in the correct order. In Swift 2, I can
easily glance at my code to check its correctness, and I can clearly see
I’m responding to the completion block correctly. In Swift 3, I need to
look elsewhere to see what the parameters in “(false, nil, nil)" refers to,
and that negates one of the key advantages that Swift has.

For this kind of closure, I feel like it’s almost required that we be able
to have parameter names. My request is that if we list the parameter names
explicitly, like in the Swift 2 example, then they appear in the closure
call by default.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161008/52f6fed3/attachment.html>


More information about the swift-evolution mailing list