Parameter names in closures in Swift 3 are no longer possible


(Andrew Hart) #1

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.


(Xiaodi Wu) #2

As mentioned previously, the core team has laid out a two-step roadmap to
restoring parameter names:

https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160711/024331.html

···

On Mon, Oct 10, 2016 at 10:55 Andrew Hart via swift-evolution < swift-evolution@swift.org> wrote:

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.
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution