# Compound variable names

**URL:** <https://forums.swift.org/t/compound-variable-names/37963>\
**Category:** Pitches\
**Created:** [June 28, 2020, 2:54am UTC](https://forums.swift.org/t/compound-variable-names/37963 "2020-06-28T02:54:31Z")\
**Posts on this page:** 20\
**Page:** 1

<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:** [June 28, 2020, 2:54am UTC](https://forums.swift.org/t/compound-variable-names/37963/1 "2020-06-28T02:54:31Z")

</div>

Hi everyone! This topic has come up several times over the years, but I've started digging into an implementation recently and wrote up a first draft of a proposal to flesh out a bunch of the details.

Specific areas where I'd like assumptions validated (or don't have the expertise to fill out all the information) have been called out in italics. I of course welcome broad feedback about the proposal, and if anyone can spot additional corners where compound name syntax will have to be explicitly supported, please don't hesitate to raise it below.

## Introduction

This proposal introduces a syntax for defining funciton-typed variables which have compound names (i.e., names with argument labels). This allows the call sites of such variables to achieve the same clarity that is achieveable with `func` declarations.

Previous discussions:  
[[Update + Commentary] SE-0111: Remove type system significance of function argument labels](https://forums.swift.org/t/update-commentary-se-0111-remove-type-system-significance-of-function-argument-labels/3391)  
[Extending declaration names for closures](https://forums.swift.org/t/extending-declaration-names-for-closures/10376)  
[Include argument labels in identifiers](https://forums.swift.org/t/include-argument-labels-in-identifiers/35367)

## Motivation

[SE-0111](https://github.com/apple/swift-evolution/blob/master/proposals/0111-remove-arg-label-type-significance.md) removed the type-system significance of argument labels, instead relegating them to part of the _name_ of closure and function variables. Shortly after acceptance, it became clear that this resulted in a usability regression. Closure parameters and variables could no longer supply argument labels to _their_ parameters, resulting in a reduction of call-site clarity:

```swift
// Pre-Swift-3:
let pow: (base: Int, exponent: Int) -> Int = { ... }
let eight = pow(base: 2, exponent: 3)

// Post-Swift-3:
let pow: (Int, Int) -> Int = { ... }
let eightOrNine = pow(2, 3)

```

The post-Swift-3 state of affairs has persisted to this day, leaving closures unable to benefit from argument labels, which are [regularly](https://medium.com/ios-os-x-development/10-things-i-like-about-swift-7bbd40cabb79) [listed](http://iosbrain.com/blog/2018/01/27/writing-expressive-meaningful-and-readable-code-in-swift-4/) amongst users' favorite features of Swift.

With some APIs introduced in the latest round of Apple's operating systems, this deficiency will become more apparent than ever. Types have begun adopting the ["struct-with-closures" pattern](https://developer.apple.com/documentation/uikit/uicollectionviewdiffabledatasource/sectionsnapshothandlers) in place of delegates/protocols, which, if the pattern continues to be used, will eventually degrade the readability of call sites throughout codebases.

## Proposed solution

This proposal would adopt a modified version of the first step of the plan Chris Lattner proposed back when SE-0111 was originally accepted (edited for formatting):

> [@\[Update + Commentary\] SE-0111: Remove type system significance of function argument labels](https://forums.swift.org/t/update-commentary-se-0111-remove-type-system-significance-of-function-argument-labels/3391/1):
>
> First, we extend declaration names for variables, properties, and parameters to allow _parameter names_ as part of their declaration name. For example:
> 
> ```swift
> var op(lhs:,rhs:) : (Int, Int) -> Int // variable or property.
> x = op(lhs: 1, rhs: 2) // use of the variable or property.
> 
> // API name of parameter is “opToUse”, internal name is "op(lhs:,rhs:)”.
> func foo(opToUse op(lhs:,rhs:) : (Int, Int) -> Int) {
> x = op(lhs: 1, rhs: 2) // use of the parameter
> }
> foo(opToUse: +) // call of the function
> 
> ```

The modification is to drop the commas from Chris's proposed syntax in order to utilize the existing syntax for referring to functions and methods with compound names.

Thus, under this proposal, Chris's example would become:

```swift
var op(lhs:rhs:): (Int, Int) -> Int
x = op(lhs: 1, rhs: 2)

func foo(opToUse op(lhs:rhs:): (Int, Int) -> Int) {
  x = op(lhs: 1, rhs: 2)
}
foo(opToUse: +)

```

The resulting call site is far clearer than if we had just the base name.

## Detailed design

### Grammar

The language grammar will be extended in several locations to accept compound names where today only identifiers are accepted. These changes to the grammar will make use of the existing `argument-names` production:

```swift
argument-names → argument-name argument-names_opt
argument-name → identifier ':'

```

Specifically, the following productions will be replaced with equivalents that accept compound names

```swift
// Old
variable-name → identifier
// New
variable-name → identifier '(' argument-names ')'

// Old
external-parameter-name → identifier
local-parameter-name → identifier
// New
external-parameter-name → identifier '(' argument-names ')'
local-parameter-name → identifier '(' argument-names ')'

// Old
tuple-element → identifier ':' expression
// New
tuple-element → identifier '(' argument-names ')' ':' expression

// Old
tuple-pattern-element → identifier ':' pattern
// New
tuple-pattern-element → identifier '(' argument-names ')' ':' pattern

// Old (tuple types)
element-name → identifier
// New
element-name → identifier '(' argument-names ')'

```

Additionally, we will introduce the following productions into the grammar :

```swift
pattern → compound-name-pattern type-annotation_opt
compound-name-pattern → identifier '(' argument-names ')'
primary-expression → identifier '(' argument-names ')'

```

Note that the last production appears to be accepted by the compiler already, although it is not listed in the language reference.

_I couldn't find anywhere in the official language documentation that allows the existing syntax for compound names in primary-expression position; is this an oversight on my part, or should it just be added now, separate from this proposal?_

### Typing rules

The typing rules are relatively straightforward:

1. Any value with a compound name must have function type (or optional function type).
2. The number of labels must match the number of arguments to the function type.

If these rules are violated, the compiler will produce a diagnostic.

If the user defines a variable with a compound name that does not have function type, a fix-it will be offered to truncate the name to just the base name. If not enough argument labels are provided, a fix-it will be offered to insert additional '`_:`' labels. If too many argument labels are provided, a fix-it will be offered to remove the extra labels.

_Offering fix-its here might be erroneous. We can't necessarily know where the excess/missing argument labels are meant to go—is it better to just not offer the fix-its at all?_

### Valid positions for compound names

Compound names can be used in any position where a function-typed variable is being given a name by which it can later be referenced. This includes the following

1. Variable declarations:

```swift
struct S {
  let f(x:): (Int) -> Void = { print($0) }
  func foo() {
    let g(y:): (Int) -> Void = { print($0) }
    g(y: 0) // 0
    self.f(x: 0) // 0
  }
}

```

1. Pattern-match bindings

```swift
enum E {
  case c(f: (Int) -> Void)
}

func foo() {
  let e: E = .c({ print($0) })
  switch e {
  case .c(let f(x:)):
    f(x: 0) // 0
  }
}

```

1. Tuple types and expressions

```swift
let t: (f(x:): (Int) -> Void, x: Int) = (f(x:): { print($0) }, x: 0)
t.f(x: t.x) // 0

```

1. Function, initializer, and subscript parameters

```swift
func foo(callback callback(data:): (Int) -> Void) {
  callback(data: 10)
}

```

_Are there any other spots in the language that I'm overlooking here?_

Compound names are not permitted to appear in any position which would result in them being the external label to a function parameter. This means that following declaration is malformed:

```swift
func fetchImage(from url: URL, callback(image:): (UIImage) -> Void) { ... }

```

There are several reasons for this rule.

- For one, it allows us to avoid the potential for unboundedly "deep" declaration names such as `f(g(h(x:):):)`.

- Additionally, though, it is the author's determination that _allowing_ constructions such as this do nothing to advance the goal of call-site clarity _for the labeled function value_. The name provided in an argument label is inherently divorced from the name used to call the function.

There may be minor benefit at the point where the function value is _passed_ with a label, but it may also potentially result in users defining functions with awkward argument labels, e.g, if they don't realize that in the above example '`callback(image:)`' is both the internal and _external_ name. There is also no clear path forward in this case to extending the use of compound names inline (see **Future directions** ).

This proposal does not close off the possibility of such labels being allowed in the future, and the author suggests that such a feature be evaluated separately from the issue at hand.

When arguments such as the above are defined, the compiler will produce a diagnostic and offer a fix-it of the form:

```swift
Error: compound names cannot be used as external argument labels.
Fix-it: Insert 'callback '

```

in order to provide non-compound argument label.

In practice, this means that enumeration cases are not allowed to use compound names for associated value labels, since these are always API.

### Calling variables with compound names

A value defined with name `f(x1:...xn:)` that has non-optional function type may be called exactly the same as a function declared as

```swift
func f(x1: T1, ..., xn: Tn) { ... }

```

The call site for `f` in both cases appears as:

```swift
f(x1: arg1, ..., xn: argn)

```

If `f(x1:...xn:)` has optional function type, the call site instead appears as:

```swift
f?(x1: arg1, ..., xn: argn)

```

### Referencing compound names

Like functions and methods, variables with compound names may be referred to by either their base name, or their full name. Thus, the following is valid:

```swift
struct S {
  var foo(x:): (Int) -> Void = {}
}

let s = S()
let foo1 = s.foo
let foo2 = s.foo(x:)

```

Since argument labels are part of the name of the variable, it is perfectly valid to define multiple variables in the same scope/type which share a base name:

```swift
struct Foo {
  let f: () -> Void
  let f(x:): (Int) -> Void
}

```

These differences should be naturally resolved when calling the function. If needed when referencing the variable, users can differentiate by spelling out the full name, such as `someFoo.f(x:)` or by providing a type annotation, e.g., `let g = f as () -> Void`. The

This proposal does not adopt any rules which allow true overloading of variables with compound names, i.e., two variables with the same base name and argument labels, but different types. Such a rule could be considered in a later proposal (see **Future directions** below).

### Synthesized initializers

Since compound names cannot be the external argument label for an initializer argument, a bit of care is required when synthesizing the initializer. This proposal would adopt a rule which simply drops the argument labels from the name of any properties which have compound names to create the argument label. In practice, this would work as follows:

```swift
// User writes:
struct S {
  let f(x:) = (Int) -> Void
  let g(y:) = (Int) -> Void
}

// Compiler synthesizes
extension S {
    init(f f(x:): (Int) -> Void, g g(y:): (Int) -> Void) {
    self.f(x:) = f(x:)
    self.g(y:) = g(y:)
  }
}

// Resulting initializer call
S(f: { print($0) }, g: { print($0 + 1) })

```

This can potentially lead to some ambiguity if two closure variables with compound names have the same base name. However, the proposal author makes the judgement that this will be unlikely in practice, and so does not justify supporting external argument labels with compound names.

Instead, the compiler can offer a warning if two compound names would have the same label in the synthesized initializer, with the option for the user to define an explicit initializer to silence the warning.

### `KeyPath` and compound names

Currently, key paths cannot refer to methods, so there is no compound name syntax in key paths. This proposal introduces the natural syntax for referring to members with compound names:

```swift
struct S {
  let f(x:): (Int) -> Void
}

let path: KeyPath<S, (Int) -> Void> = \.f(x:)

```

## Source compatibility

This is a purely additive proposal and maintains full source compatibility.

## Effect on ABI stability

_TBC: I haven't reached the point in my implementation where I have had to make any changes to ABI-dependent structures, so I can't yet comment on the impact here. Anyone who is more knowledgable on this front, I encourage you to call out any major issues you anticipate!_

## Effect on API resilience

Changing the name of a `public` symbol is an API-breaking change, and would remain so under this proposal. Specifically, adding or removing argument labels—or changing an existing argument label—is not API-safe in the general case.

## Future directions

### Allow declaration of argument labels inline with the function type

With this feature fully implemented, we can move to the second step raised in the post-acceptance discussion of SE-0111. This would allow a declaration such as:

```swift
var pow: (base: Int, exponent: Int) -> Int = { ... }

```

to be a syntactic sugar for

```swift
var pow(base:exponent:): (Int, Int) -> Int = { ... }

```

This should be a relatively straightforward extension of the groundwork laid in this proposal.

### Allow external argument labels to have compound names as well

As discussed in **Valid positions for compound names** , external argument labels cannot have compound names, so the following declaration and call are invalid:

```swift
func fetchImage(from url: URL, callback(image:): (UIImage) -> Void) { ... }

// ...

fetchImage(from: someURL, callback(image:): {
  imageView.image = $0
})

```

### Enable overloading for variables with compound names

This proposal supports variables/properties which share a base name, such as:

```swift
struct Foo {
  let f: (Int) -> Void
  let f(x:): (Int) -> Void
}

```

In theory, this could be extended to allow overloading of these properties, so that one could write:

```swift
struct Foo {
  let f(x:): (String) -> Void
  let f(x:): (Int) -> Void
}

```

Allowing the type context at the call/reference site to disambiguate.

### Extend `optional` requirement support for protocol requirements

Currenly, the `optional` modifier for protocol requirements may only be applied to member of `@objc` protocols. However, since compound function labels can satisfy protocol requirements, and can also be applied to optional function types, one could imagine a pure Swift version of `optional`, where the protocol:

```swift
protocol P {
  optional func foo(x: Int)
}

```

is roughly equivalent to something like:

```swift
protocol P {
  var foo(x:): ((Int) -> Void)? { get }
}

extension P {
  var foo(x:): ((Int) -> Void)? { nil }
}

```

@DevAndArtist has raised this possibility before, and with the addition of compound variable names it would become a generally applicable feature for any function requirement.

---

<div class="post-metadata">

**Author:** ![typesanitizer](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/typesanitizer/32/8661_2.png) [@typesanitizer](https://forums.swift.org/u/typesanitizer)\
**Post date:** [June 28, 2020, 3:20am UTC](https://forums.swift.org/t/compound-variable-names/37963/2 "2020-06-28T03:20:30Z")

</div>

> [@Jumhyn](#):
>
> The typing rules are relatively straightforward:
> 
> 1. Any value with a compound name must have function type (or optional function type).
> 2. The number of labels must match the number of arguments to the function type.

Is the omission of `callAsFunction` here intentional or accidental? (And if it is intentional, what is the reason?)

---

<div class="post-metadata">

**Author:** ![anandabits](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anandabits/32/355_2.png) [@anandabits](https://forums.swift.org/u/anandabits)\
**Post date:** [June 28, 2020, 3:27am UTC](https://forums.swift.org/t/compound-variable-names/37963/3 "2020-06-28T03:27:33Z")

</div>

> [@Jumhyn](#):
>
> Currently, key paths cannot refer to methods, so there is no compound name syntax in key paths. This proposal introduces the natural syntax for referring to members with compound names:
> 
> ```swift
> struct S {
> let f(x:): (Int) -> Void
> }
> 
> let path: KeyPath<S, (Int) -> Void> = \.f(x:)
> 
> ```

Isn’t this syntax we have discussed using to access unhurried unbound methods?

---

<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:** [June 28, 2020, 3:28am UTC](https://forums.swift.org/t/compound-variable-names/37963/4 "2020-06-28T03:28:59Z")

</div>

Sorry, I’m not seeing an immediate connection between the portion you’ve highlighted and `callAsFunction`—would you mind expanding a bit?

---

<div class="post-metadata">

**Author:** ![typesanitizer](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/typesanitizer/32/8661_2.png) [@typesanitizer](https://forums.swift.org/u/typesanitizer)\
**Post date:** [June 28, 2020, 3:35am UTC](https://forums.swift.org/t/compound-variable-names/37963/5 "2020-06-28T03:35:35Z")

</div>

If a type `T` has a `callAsFunction` method of type `(A1 ... AN) -> R`, then one can "call" an instance `t : T` with arguments of type `(A1 ... AN)`. ~~So the user-facing behavior is as-if `T` is a subtype of the function type `(A1 ... AN) -> R`~~ (EDIT: This is incorrect, see Jumhyn's comment below). I was asking if that is supposed to work for arguments as well. For example, will you be able to have an argument like `f f(x:): T` where `T` is a nominal type with a `callAsFunction` method with a single argument? The pitch mentions "must have function type" which excludes such nominal types.

---

<div class="post-metadata">

**Author:** ![Dante-Broggi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dante-broggi/32/7955_2.png) [@Dante-Broggi](https://forums.swift.org/u/Dante-Broggi)\
**Post date:** [June 28, 2020, 3:35am UTC](https://forums.swift.org/t/compound-variable-names/37963/6 "2020-06-28T03:35:37Z")

</div>

I presume the following would be a redeclaration error, but this should be spelled out in the proposal either way:

```swift
struct S {
  let f(x:): (Int) -> Void
  func f(x: Int) -> Void {} // ERROR: Redeclaration of `f(x:)`
}

```

---

<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:** [June 28, 2020, 3:43am UTC](https://forums.swift.org/t/compound-variable-names/37963/7 "2020-06-28T03:43:36Z")

</div>

Ah! Thanks for spelling that out. AFAIK we don't today allow conversions to function types from types with matching `callAsFunction` signatures, e.g., this fails:

```swift
struct S {
  func callAsFunction(x: Int) { print(x) }
}

let s = S()
s(x: 0)

func foo(f: (Int) -> Void) {
  f(10)
}

foo(f: s) // Error: Cannot convert value of type 'S' to expected argument type '(Int) -> Void'

```

Am I missing something?

---

<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:** [June 28, 2020, 3:47am UTC](https://forums.swift.org/t/compound-variable-names/37963/8 "2020-06-28T03:47:46Z")

</div>

> [@anandabits](#):
>
> Isn’t this syntax we have discussed using to access unhurried unbound methods?

I haven't seen those discussions personally, but that does seem like a reasonable syntax for such a feature. I'm not troubled by that, really, since already members with compound names and bound methods would be accessed via the same syntax:

```swift
struct S {
  var f(x:): (Int) -> Void = {}
  func g(x: Int) {}
}

let f = S().f(x:)
let g = S().g(x:)

```

> [@Dante-Broggi](#):
>
> I presume the following would be a redeclaration error, but this should be spelled out in the proposal either way:
> 
> ```swift
> struct S {
> let f(x:): (Int) -> Void
> func f(x: Int) -> Void {} // ERROR: Redeclaration of `f(x:)`
> }
> 
> ```

Good callout, I think you're right about the behavior there (but still interested to hear what others think!). I'll be sure to include something about this situation.

---

<div class="post-metadata">

**Author:** ![typesanitizer](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/typesanitizer/32/8661_2.png) [@typesanitizer](https://forums.swift.org/u/typesanitizer)\
**Post date:** [June 28, 2020, 3:49am UTC](https://forums.swift.org/t/compound-variable-names/37963/9 "2020-06-28T03:49:53Z")

</div>

Ah. So my subtyping claim is incorrect. My bad, I should've checked. But the question about it being permissible for types with `callAsFunction` still remains. Should the following compile?

```swift
struct S {
  func callAsFunction(x: Int) { print(x) }
}
func foo(f f(x:): S) {
  f(x: 10)
}

```

---

<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:** [June 28, 2020, 3:57am UTC](https://forums.swift.org/t/compound-variable-names/37963/10 "2020-06-28T03:57:50Z")

</div>

I lean towards "no" since we don't have more general equivalence between "callable" types and function types with matching parameter/return types. Further, these callable types don't suffer from the same problem as closures generally:

```swift
struct S {
  func callAsFunction(x: Int) { print(x) }
}

func foo(f: S) {
  f(x: 10) // This already works!
}

```

Also, there are more difficult questions that would have to be answered, such as what to do with a type that defines multiple `callAsFunction` methods:

```swift
struct S {
    func callAsFunction(x: Int) { print(x) }
    func callAsFunction(y: String) { print(y) }
    func callAsFunction(a: String, b: Int) { print(a); print(b) }
}

func foo(f(x:): S) {
    f(x: 10)
    f(x: "") // ?
    f(x: "", b: 0) // ??
}

```

Overall, I think this question is better left to a time when (if?) we want some sort of conversion/equivalence for these types.

---

<div class="post-metadata">

**Author:** ![SwiftUI](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/swiftui/32/24361_2.png) [@SwiftUI](https://forums.swift.org/u/SwiftUI)\
**Post date:** [June 28, 2020, 5:13am UTC](https://forums.swift.org/t/compound-variable-names/37963/11 "2020-06-28T05:13:04Z")

</div>

Nice advice, but not necessary to current syntax system; more harder to write but less benefits to get for developers.  
-0.5 for now

---

<div class="post-metadata">

**Author:** ![cukr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cukr/32/889_2.png) [@cukr](https://forums.swift.org/u/cukr)\
**Post date:** [June 28, 2020, 10:21am UTC](https://forums.swift.org/t/compound-variable-names/37963/12 "2020-06-28T10:21:10Z")

</div>

I love it. Argument labels are one of the coolest features of swift, and I was annoyed that you lose them when you pass functions around. Your solution is the ideal one in my opinion.

I'm neutral about if Xcode should offer a fixit for removing/adding labels

Can we make it so internal labels are used to guide the autocompletion, so that you don't have to guess which argument of a callback is which?

```swift
// today
func takesCallback(callback: (Int, Int, Int) -> Void) { }
takesCallback { (<#Int#>, <#Int#>, <#Int#>) in
    <#code#>
}
// future?
func takesCallback(callback callback(a:b:c:): (Int, Int, Int) -> Void) { }
takesCallback { (a, b, c) in
    <#code#>
}

```

---

<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:** [June 28, 2020, 1:18pm UTC](https://forums.swift.org/t/compound-variable-names/37963/13 "2020-06-28T13:18:10Z")

</div>

I really like that. I’m not actually clear on how much of code completion falls under Swift Evolution, but if that’s in-scope then I’m all for it!

---

<div class="post-metadata">

**Author:** ![anandabits](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anandabits/32/355_2.png) [@anandabits](https://forums.swift.org/u/anandabits)\
**Post date:** [June 28, 2020, 3:10pm UTC](https://forums.swift.org/t/compound-variable-names/37963/14 "2020-06-28T15:10:20Z")

</div>

> [@Jumhyn](#):
>
> I'm not troubled by that, really, since already members with compound names and bound methods would be accessed via the same syntax:
> 
> ```swift
> struct S {
> var f(x:): (Int) -> Void = {}
> func g(x: Int) {}
> }
> 
> let f = S().f(x:)
> let g = S().g(x:)
> 
> ```

This uses different syntax which does not involve key paths. It wouldn’t make sense to access an unbound method from an instance like this. The syntax that has been discussed has always been using keypath syntax.

Here are a couple of threads that have included discussions of this feature:

> [@Introduce flattened unapplied method references (SE-0042 revisited)](https://forums.swift.org/t/introduce-flattened-unapplied-method-references-se-0042-revisited/11765):
>
> Introduction In [SE-0042](https://github.com/apple/swift-evolution/blob/master/proposals/0042-flatten-method-types.md) @Joe_Groff introduced the idea of flattening unapplied method references (UMRs), from the curried form to a function with an additional Self parameter (the details can be found in the proposal - it's a quick and easy read). This proposal was accepted, yet never implemented. A benefit resulting from this change would be a massive improvement when passing around methods as function parameters. As Joe Groff himself puts it: For instance, though you can pass the global + …

> [@Allow key paths to reference unapplied instance methods](https://forums.swift.org/t/allow-key-paths-to-reference-unapplied-instance-methods/35582):
>
> [Allow key paths to reference unapplied instance methods](https://github.com/pcantrell/swift-evolution/blob/method-keypaths/proposals/NNNN-method-keypaths.md) The [original key path proposal](https://github.com/apple/swift-evolution/blob/master/proposals/0161-key-paths.md) expressly limited key paths to be able to reference only properties and subscripts: \Person.name // KeyPath\<Person, String\> \Person.pets[0] // KeyPath\<Person, Pet\> This proposal adds the ability for key paths to reference instance methods, optionally specifying argument names: \Person.sing // KeyPath\<Person, () -\> Sound\> \Person.sing(melody:lyrics:) // KeyPat…

---

<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:** [June 28, 2020, 3:44pm UTC](https://forums.swift.org/t/compound-variable-names/37963/15 "2020-06-28T15:44:25Z")

</div>

> [@anandabits](#):
>
> This uses different syntax which does not involve key paths. It wouldn’t make sense to access an unbound method from an instance like this. The syntax that has been discussed has always been using keypath syntax.

I understand the feature you're talking about, and I wasn't trying to propose an alternate key path syntax for unbound methods—my point was about the syntax used today for normal instance-member access of _bound_ methods.

I took your objection to be something like, "unbound method key paths already are expected to use the `\.f(x:)` syntax, so this proposal would potentially introduce a conflict." My response was meant to illustrate that, in a future world with both compound-name members and unbound method key paths, I see no issue with using the same syntax to refer to both, since under this proposal we would already use the same syntax to refer to both _bound_ methods and compound-name members on instances.

To be more concrete, I see no problem with the following:

```swift
struct S {
  var f(x:): (Int) -> Void = {}
  func g(x: Int) {}
}

let kpf: KeyPath<S, (Int) -> Void> = \.f(x:)
let kpg: KeyPath<S, (Int) -> Void> = \.g(x:)

```

In fact, I would _insist_ that we should use the same syntax for both precisely because it mirrors the syntax of a normal instance-member access. Additionally, if we adopt @Dante-Broggi's suggestion of disallowing both `var f(x:): (Int) -> Void` and `func f(x: Int) {}` in the same type, then we would never even have to worry about conflicting key paths between unbound methods and compound-name members.

If the above is not an acceptable future state of affairs, then IMO neither is

```swift
let f = S().f(x:)
let g = S().g(x:)

```

and so the **Referencing compound names** section of this proposal would need to be rethought.

If I've misunderstood your objection, would you mind elaborating a bit?

---

<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:** [June 28, 2020, 4:04pm UTC](https://forums.swift.org/t/compound-variable-names/37963/16 "2020-06-28T16:04:17Z")

</div>

@cukr I've thought about this a bit more and have a couple of questions. Mainly, I'm concerned that this code completion behavior would place _external_ argument labels as the internal names for the closure, which may not be the right behavior. E.g.,

```swift
// Public interface
func getDownloadURL(then initiateRequest(with:): (URL) -> Void)

// Code completion
getDownloadURL { with in
  <#code#>
}

```

Given that the argument labels in compound names are meant to be used at the call site, its not clear to me that this would result in helpful (internal) parameter names for the closure itself.

Since we don't have any data currently on how this might be used (unless we look back at pre-Swift-3 code...?), maybe it would be better to wait to see how this feature gets used before we start messing with code completion?

---

<div class="post-metadata">

**Author:** ![anandabits](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anandabits/32/355_2.png) [@anandabits](https://forums.swift.org/u/anandabits)\
**Post date:** [June 28, 2020, 4:09pm UTC](https://forums.swift.org/t/compound-variable-names/37963/17 "2020-06-28T16:09:51Z")

</div>

> [@Jumhyn](#):
>
> To be more concrete, I see no problem with the following:
> 
> ```swift
> struct S {
> var f(x:): (Int) -> Void = {}
> func g(x: Int) {}
> }
> 
> let kpf: KeyPath<S, (Int) -> Void> = \.f(x:)
> let kpg: KeyPath<S, (Int) -> Void> = \.g(x:)
> 
> ```

This is not correct. The signature of the unbound, uncurried form of `g` is `(S, Int) -> Void`. It would not typically be used in a context expecting a key path, but if it were, it would need to be a key path to a static member, i.e. `KeyPath<S.Type, (S, Int) -> Void>`.

I think you do misunderstand my comments. I do not necessarily have an _objection_, just something for _consideration_. I want to make sure we don’t introduce a feature that would make it more difficult to eventually provide access to unbound, uncurried methods (including `mutating` methods). This has been a frustrating gap in the language for a long time.

@Joe_Groff do you have any thought on whether this pitch would conflict with the eventual use of key path syntax for accessing unbound, uncurried methods?

---

<div class="post-metadata">

**Author:** ![cukr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cukr/32/889_2.png) [@cukr](https://forums.swift.org/u/cukr)\
**Post date:** [June 28, 2020, 4:29pm UTC](https://forums.swift.org/t/compound-variable-names/37963/18 "2020-06-28T16:29:40Z")

</div>

Good point. Let's not do that yet.

---

<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:** [June 28, 2020, 4:46pm UTC](https://forums.swift.org/t/compound-variable-names/37963/19 "2020-06-28T16:46:33Z")

</div>

> [@anandabits](#):
>
> This is not correct. The signature of the unbound, uncurried form of `g` is `(S, Int) -> Void` . It would not typically be used in a context expecting a key path, but if it were, it would need to be a key path to a static member, i.e. `KeyPath<S.Type, (S, Int) -> Void>` .

Ah! Thank you for making that clear. IMO, if type context is sufficient to differentiate between the bound method key path `\.f(x:)` and the unbound, uncurried method key path `\.f(x:)` then I _still_ don't see much of an issue with overloading the syntax—a user could always differentiate as `\S.f(x:)` and `\S.Type.f(x:)` if necessary.

OTOH, if overloading the syntax in this manner turns out to be an issue in practice, I personally would _much_ rather see `\.f(x:)` be used to refer to instance members and bound methods. Given that we already have the `S().f(x:)` syntax, it would be very disappointing if `\.f(x:)` did not refer to a key path to the same method.

If unbound, uncurried key path syntax fits into that story, then great! But if not, I would rather extend the existing syntax to bound methods in the most natural way, and introduce a new "flatten" operator or something that could be used on `S.f(x:)` to access the unbound, uncurried method.

---

<div class="post-metadata">

**Author:** ![anandabits](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anandabits/32/355_2.png) [@anandabits](https://forums.swift.org/u/anandabits)\
**Post date:** [June 28, 2020, 6:23pm UTC](https://forums.swift.org/t/compound-variable-names/37963/20 "2020-06-28T18:23:53Z")

</div>

> [@Jumhyn](#):
>
> If unbound, uncurried key path syntax fits into that story, then great! But if not, I would rather extend the existing syntax to bound methods in the most natural way, and introduce a new "flatten" operator or something that could be used on `S.f(x:)` to access the unbound, uncurried method.

It is not possible to access a `mutating` method in curried form so an uncurry operator is not a viable solution. We need new syntax for accessing an unbound, uncurried method.

[Next page](https://forums.swift.org/t/compound-variable-names/37963.md?page=2)
