Can first trailing closure be named?

If I have the following function:

Foobar(connecting: {
  connectingEvent()
} disconnecting: {
  disconnectingEvent()
})

I can rewrite as trailing closures:

Foobar() {
  connectingEvent()
} disconnecting: {
  disconnectingEvent()
})

But how can I keep the connecting: label on the first trialing closure?

You sadly can’t, it was a futur direction in the proposal that enabled multi closure syntax. There was some proponents but not enough. I think the door is not closed but one would need to write a proposal and an implementation to get some traction. For now I personally avoid using multiclosures and wrap the whole things into braces which is painful as I can’t double click.

I believe @xwu had a proposal, if not an implementation, but it never got picked up. It would likely take considerable energy to try and drive it through the evolution process. Personally I've accepted we'll never get the capability and this ergonomic misstep will live alongside unlabeled closure parameters as an unfortunate loss in readability in the language.

3 Likes

Yup, both (the implementation is now stale, but it was not very hard). To this day, it's the most popular thing I've ever posted—

2 Likes

The omission of the [optional] first trailing closure was very unpopular, to say the least. To say "there aren't enough proponents" isn't entirely true and I feel like the only way this would be considered is if other language features almost forced the first trailing closure to appear/be used.

1 Like

Sorry it seems like I completely missed Xiaodi post and proposal. I genuinely thought the discussion ended after the proposal. I even tried to revive the subject in the Swift 6 things to revisit subject. My apologies.

edit: brain fart I event replied in this thread

1 Like

Would love to see someone to pick this up. Such a missed opportunity.

2 Likes

I'm still confused why it wasn't even part of the original feature scope to begin with. I guess working with real open source languages has spoiled me a bit when it comes to these FAANG projects