# Extending declaration names for closures

**URL:** <https://forums.swift.org/t/extending-declaration-names-for-closures/10376>\
**Category:** Pitches\
**Tags:** implementers-needed\
**Created:** [February 27, 2018, 8:41am UTC](https://forums.swift.org/t/extending-declaration-names-for-closures/10376 "2018-02-27T08:41:16Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [February 27, 2018, 8:41am UTC](https://forums.swift.org/t/extending-declaration-names-for-closures/10376/1 "2018-02-27T08:41:16Z")

</div>

[SE-0111](https://github.com/apple/swift-evolution/blob/master/proposals/0111-remove-arg-label-type-significance.md) was accepted with a small temporary regression of loosing parameter labels in closures. In an [extra commentary](https://lists.swift.org/pipermail/swift-evolution-announce/2016-July/000233.html) to this regression the core team pitched a solution that would be purely additive to the language without being a burden for the type system like pre SE-0111.

The solution has to come in two steps:

1. We have to extend the declaration names to allow labels in parentheses for variables, properties and parameters. (The following code snippet does not use `,` in between labels because in my honest opinion this is unnecessary and even inconsistent to how we already reference to functions `foo(first:second)` today.)

2. We extend the rules for function types to allow API labels to simplify the syntax from above. (This step would be pure sugar over the first step and won't replace it.)

I personally think while the second step would be nice, it is complementary to the core functionality introduced by the first step. Thus we should keep our focus on the main functionality.

However, I'm very open minded in that regard. That said, if someone can tackle the implementation of both steps for Swift 5 that would be great.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [February 27, 2018, 8:48am UTC](https://forums.swift.org/t/extending-declaration-names-for-closures/10376/2 "2018-02-27T08:48:37Z")

</div>

Furthermore I think we also should allow multi-line declaration names when they contain parentheses similar to how we can break down a closure type into multiple line when necessary (for instance in a 80 character line width code base):

```swift
var operate(
  first:
  second:
  third:
  fourth:
  fifth:
): (
  SomeType,
  OtherType,
  SomeVeryLongNameForAType,
  YetAnotherType,
  LastType
) -> Void = ...

```

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [February 27, 2018, 9:33am UTC](https://forums.swift.org/t/extending-declaration-names-for-closures/10376/3 "2018-02-27T09:33:06Z")

</div>

I have been looking forward to this for a long time, and I’m glad you brought it up.

> [@DevAndArtist](#):
>
> I personally think while the second step would be nice, it is complementary to the core functionality introduced by the first step. Thus we should keep our focus on the main functionality.
> 
> However, I’m very open minded in that regard. That said, if someone can tackle the implementation of both steps for Swift 5 that would be great.

While you are technically correct that the second step would be entirely syntax sugar, your last example shows how important it can be to keep the labels adjacent to the types:

> [@DevAndArtist](#):
>
> ```swift
> var operate(
> first:
> second:
> third:
> fourth:
> fifth:
> ): (
> SomeType,
> OtherType,
> SomeVeryLongNameForAType,
> YetAnotherType,
> LastType
> ) -> Void = ...
> 
> ```

It is not obvious at a glance which label corresponds to which type. It would be much easier to read and understand if written as:

```swift
var operate : (
  first : SomeType,
  second: OtherType,
  third : SomeVeryLongNameForAType, 
  fourth: YetAnotherType,
  fifth : LastType
) -> Void = ...

```

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [February 27, 2018, 9:35am UTC](https://forums.swift.org/t/extending-declaration-names-for-closures/10376/4 "2018-02-27T09:35:06Z")

</div>

Yeah sorry that was a typo, I added `-> Void` now.

You are correct that it is not obvious but this should be supported anyways even if step 2 will refine the syntax from the readability standpoint.

---

<div class="post-metadata">

**Author:** ![Panajev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/panajev/32/1227_2.png) [@Panajev](https://forums.swift.org/u/Panajev)\
**Post date:** [February 27, 2018, 9:47am UTC](https://forums.swift.org/t/extending-declaration-names-for-closures/10376/5 "2018-02-27T09:47:00Z")

</div>

Thank you Adrian for resurrecting this topic.  
I remember that thread was quite heated as more people became aware of the changes and the regression and maybe there were a bit over the top emotions sometimes, but the issue was concerning enough and the core team re-discussed and agreed a fix was needed and that the situation was temporary. I am afraid that source and ABI stability goals may kill this for good which I think is a huge wasted opportunity for the language.

It would be good to hear core maintainers chime on this again please, this is not an easy community task, but it something the language needs and we lost a big push for clarity at call site and we should gain it back.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [February 27, 2018, 9:48am UTC](https://forums.swift.org/t/extending-declaration-names-for-closures/10376/6 "2018-02-27T09:48:41Z")

</div>

#### Future direction that is orthogonal to this pitch:

##### Introduction of `optional` function to pure Swift

The extended declaration names and the sugar from the second step together with an automatic extension synthesizing could allow us to introduce `optional` function to Swift protocols. (Possibly it will allow us to drop `@objc` attribute for optional functions as well.)

```swift
protocol P {
   optional func foo(first: Int, second: String) -> SomeReturnType
}

// The compiler would synthesize the following extension:
extension P {
   // If we only had (1) from the original pitch
   var foo(first:second:): ((Int, String) -> SomeReturnType)? {
     return nil
   }

  // If we had both (1) and (2)
  var foo: ((first: Int, second: String) -> SomeReturnType)? {
    return nil
  }
}

```

---

<div class="post-metadata">

**Author:** ![DeFrenZ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/defrenz/32/827_2.png) [@DeFrenZ](https://forums.swift.org/u/DeFrenZ)\
**Post date:** [February 27, 2018, 12:10pm UTC](https://forums.swift.org/t/extending-declaration-names-for-closures/10376/7 "2018-02-27T12:10:25Z")

</div>

IIRC the `optional` attribute of `protocol` requirements was discussed a couple of times already and dismissed as not fitting with Swift.  
It makes more sense imho in ObjC where the nature of objects is very dynamic anyway.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [February 27, 2018, 1:03pm UTC](https://forums.swift.org/t/extending-declaration-names-for-closures/10376/8 "2018-02-27T13:03:52Z")

</div>

* * *

_Everything in this particular reply is orthogonal to the original pitch, but is a possible consequence from it. We should not debated about that intensively now because it is subject of a future discussion assuming the original pitch made into the language. However I just wanted to mention it here, so that everyone is aware of it._

* * *

The first part is only partly correct. Indeed it was discussed before but in a different context, which would align it with the behaviour of Objective-C. However I showed a different approach which would be mostly pure sugar like the second step is from the original post.

A protocol requirement marked as optional will tell the compiler to synthesize an extension with a default implementation that returns `nil` for the whole closure. Nothing really special here. The only extra thing that requires support is the infix `?` between the function name and the parameter list `foo?(first: 42, second: "Swift")`. That should work because it has also been discussed whether or not protocol requirement should be satisfied with closures that uses this extended declaration names, and the idea was supported.

```swift
protocol Q {
   func bar(label: Int)
}

struct S : Q {
   var bar(label:): (Int) -> Void

   init(_ closure: (Int) -> Void) {
     self.bar(label:) = closure
   }
}

```

In fact this should also work the other way around, as long as only a getter is required:

```swift
protocol R {
   var bar(label:): (Int) -> Void { get }
}

struct T : R {
   func bar(label x: Int) { ... }
}

```

The only question is what will we do about optional closures? Are those allowed to use the extended names?

```swift
var something(label:): ((Int) -> Void)? = ... // Is this allowed?

```

If this is valid then making `optional` as pure sugar is only natural.

However if it's not there is still a different way to synthesize the extension.

```swift
protocol P {
  // non-optional return type
  optional func foo(first: Int) -> Result
  // optional return type
  optional func bar(first: Int) -> Result?
}

extension P {
   // return type becomes optional
   var foo(first:): (Int) -> Result? {
     return { _ in nil }
   }
   
   // return is again wrapped into an optional (consistent behavior)
   var bar(first:): (Int) -> Result?? {
     return { _ in nil }
   }
}

// usage of optional functions requires an infix `?` or `!`
let some1: Result? = p.foo?(first: 1)
let some2: Result = p.foo!(first: 1)

let some3: Result?? = p.bar?(first: 2)
let some4: Result? = p.bar!(first: 2)
let some4: Result = p.bar!(first: 2)!

```

---

<div class="post-metadata">

**Author:** ![MutatingFunk](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mutatingfunk/32/984_2.png) [@MutatingFunk](https://forums.swift.org/u/MutatingFunk)\
**Post date:** [February 27, 2018, 1:22pm UTC](https://forums.swift.org/t/extending-declaration-names-for-closures/10376/9 "2018-02-27T13:22:55Z")

</div>

I support extending declaration names, but I think you're getting ahead of yourself by bringing up optional protocol methods. The former is a widely desired feature with approval from the core team; the latter a controversial subject, requiring a feature which is not yet implemented. The general response from past discussion seems negative (with some going so far as to suggest [eliminating optional methods entirely](https://forums.swift.org/t/idea-how-to-eliminate-optional-protocol-requirements/2095)), so you're probably not doing yourself any favours by mentioning it as a future direction.

---

<div class="post-metadata">

**Author:** ![MutatingFunk](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mutatingfunk/32/984_2.png) [@MutatingFunk](https://forums.swift.org/u/MutatingFunk)\
**Post date:** [February 27, 2018, 1:31pm UTC](https://forums.swift.org/t/extending-declaration-names-for-closures/10376/10 "2018-02-27T13:31:14Z")

</div>

That said, from the same topic we have your suggestion from Joe Groff:

> [@\[Idea\] How to eliminate 'optional' protocol requirements](https://forums.swift.org/t/idea-how-to-eliminate-optional-protocol-requirements/2095/7):
>
> To me, compound names for closure properties and satisfying property requirements with methods aren't hacks, they're missing features we ought to support anyway. I strongly prefer implementing those over your proposed solution. It sounds to me like a lot of people using optional protocol requirements _want_ the locality of control flow visible in the caller, for optimization or other purposes, and your proposed solution makes this incredibly obscure and magical.

We may get the feature, but I still wouldn't bring it up this far in advance.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [February 27, 2018, 1:31pm UTC](https://forums.swift.org/t/extending-declaration-names-for-closures/10376/11 "2018-02-27T13:31:43Z")

</div>

Thank you for finding that post, interestingly enough non of the points Douglas Gregor made remain true post SE-0111, they were true before SE-0111 but are no longer correct. He describes the workarounds as "hacks", but those workarounds won't be any hacks anymore if the original pitch from the fist post will be part of the language.  
In that regard I don't think it does hurt anyone if I shared my ideas about `optional` requirements in this context. ;)

---

<div class="post-metadata">

**Author:** ![MutatingFunk](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mutatingfunk/32/984_2.png) [@MutatingFunk](https://forums.swift.org/u/MutatingFunk)\
**Post date:** [February 27, 2018, 1:45pm UTC](https://forums.swift.org/t/extending-declaration-names-for-closures/10376/12 "2018-02-27T13:45:59Z")

</div>

This closing statement sums up the negativity around this topic:

> [@\[Idea\] How to eliminate 'optional' protocol requirements](https://forums.swift.org/t/idea-how-to-eliminate-optional-protocol-requirements/2095/36):
>
> From the perspective of the person making their type conform to a protocol (what you’re calling a protocol “user”), there is absolutely no difference between “has a default implementation” and “is an optional requirement”.

The main thing that can be brought to the discussion at this point isn't how it would work, but rather what the use-case is. At some point it may be a simple sugar feature, or even a natural consequence of other improvements, but it just doesn't seem to carry it's weight as an independent feature.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [February 27, 2018, 2:00pm UTC](https://forums.swift.org/t/extending-declaration-names-for-closures/10376/13 "2018-02-27T14:00:37Z")

</div>

I have to disagree with that statement from Douglas because the API describes difference semantics. Sometimes it's impossible to have a default return value for the default implementation and then the whole requirement must become optional, either by moving it to a different protocol and using existentials when required, or by making the non-optional return type optional and provide a default implementation that returns `nil`. However the latter breaks the _optional requirement_ itself, because it's not the return type that should be optional but rather the whole function. The user that implements the requirement then definitely will wonder why the return type is made optional where it really shouldn't be. Yes it is fact that an _optional requirement_ affects the return type but it shouldn't be seen as if a functions return type must become optional in a regular protocol requirement.

```swift
protocol Proto {
   optional func foo()
   func bar() -> Void?

   optional baz() -> Int
   func mad() -> Int?
}

```

These functions `foo` and `bar`, `baz` and `mad` has different semantics even if the returning type in every case is the same `Void?` or `Int?`.

Anyways, this is orthogonal and we should focus on the main issue. 🤓

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [February 27, 2018, 3:48pm UTC](https://forums.swift.org/t/extending-declaration-names-for-closures/10376/14 "2018-02-27T15:48:24Z")

</div>

Thanks for bringing this up! I definitely want to see this happen soon. I don't think implementing #2 would be that much more work than just #1, and would greatly improve the ergonomics of closures with compound names in their most common use cases.

---

<div class="post-metadata">

**Author:** ![Alexey](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@Alexey](https://forums.swift.org/u/Alexey)\
**Post date:** [March 13, 2018, 3:06pm UTC](https://forums.swift.org/t/extending-declaration-names-for-closures/10376/15 "2018-03-13T15:06:00Z")

</div>

> [@Panajev](#):
>
> It would be good to hear core maintainers chime on this again please, this is not an easy community task, but it something the language needs and we lost a big push for clarity at call site and we should gain it back.

+1, it would be great if argument labels return to closures

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [April 6, 2018, 11:13am UTC](https://forums.swift.org/t/extending-declaration-names-for-closures/10376/16 "2018-04-06T11:13:51Z")

</div>

Any suggestions on how we can drive this topic forward?

---

<div class="post-metadata">

**Author:** ![Alexey](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@Alexey](https://forums.swift.org/u/Alexey)\
**Post date:** [November 12, 2018, 10:48am UTC](https://forums.swift.org/t/extending-declaration-names-for-closures/10376/17 "2018-11-12T10:48:49Z")

</div>

Swift 5 getting closer and closer but we still have no argument labels in closures.

It would be great to hear about any closure argument label plans in context of Swift 5 release. Lack of arguments labels in closures confuses a bit and I think it is very important changes in syntax to be included in Swift 5.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [September 21, 2021, 3:09pm UTC](https://forums.swift.org/t/extending-declaration-names-for-closures/10376/18 "2021-09-21T15:09:00Z")

</div>

Bumping this thread so maybe someone can pick up the topic again! 🙂

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [September 21, 2021, 4:00pm UTC](https://forums.swift.org/t/extending-declaration-names-for-closures/10376/19 "2021-09-21T16:00:43Z")

</div>

I posted a more comprehensive pitch [here](https://forums.swift.org/t/compound-variable-names/37963) last year to try to move this forward, and have a (now outdated) PR up for some initial refactoring work [here](https://github.com/apple/swift/pull/33726) that never got reviewed and so never moved forward. AFAIK this is the latest activity on this topic.

I'm still interested in picking this back up as soon as I have the time, though that shouldn't discourage anyone else from jumping on it in the meantime! 🙂

---

<div class="post-metadata">

**Author:** ![tiam](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tiam/32/7155_2.png) [@tiam](https://forums.swift.org/u/tiam)\
**Post date:** [September 30, 2021, 2:41am UTC](https://forums.swift.org/t/extending-declaration-names-for-closures/10376/21 "2021-09-30T02:41:45Z")

</div>

I sometimes use a struct with a `callAsFunction(...)` method instead of using a closure.  
You gain argument labels at the call site, but it requires more code than simply passing closures around.  
Improved clarity but less brevity.
