# Require parameter names when referencing to functions

**URL:** <https://forums.swift.org/t/require-parameter-names-when-referencing-to-functions/27048>\
**Category:** Pitches\
**Tags:** collection\
**Created:** [July 17, 2019, 2:35am UTC](https://forums.swift.org/t/require-parameter-names-when-referencing-to-functions/27048 "2019-07-17T02:35:40Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![beccadax](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/beccadax/32/34217_2.png) [@beccadax](https://forums.swift.org/u/beccadax)\
**Post date:** [July 17, 2019, 8:53pm UTC](https://forums.swift.org/t/require-parameter-names-when-referencing-to-functions/27048/21 "2019-07-17T20:53:36Z")

</div>

> [@Paul\_Cantrell](#):
>
> How should it handle no-args functions? In other word, consider:
> 
> ```swift
> struct Foo {
> func noArgsFunc() { }
> }
> let foo = Foo()
> 
> ```
> 
> …how should we get a reference to `noArgsFunc` ? The obvious answer is to keep the current precedent:
> 
> ```swift
> foo.noArgsFunc
> 
> ```
> 
> …but the non-uniformity is troubling:
> 
> ```swift
> foo.noArgsFunc // no parens
> foo.oneArgFunc(x:) // parens
> foo.unlabeledArgsFunc(_:_:_:) // parens
> 
> ```
> 
> Following the pattern of other function refs, the syntax should be `foo.noArgsFunc()` … which is of course indistinguishable from a function call.

It is non-uniform, but there's no way around that without changing `(x:y:)` to something else, like `[x:y:]`. I don't think a change like that currently is, or should be, on the table.

> [@jawbroken](#):
>
> This is one area where a keypath syntax for methods could help out a lot.

I think the idea of the keypath syntax for methods was that it would get some representation of an unbound instance method from a type, e.g. `\String.removeFirst(_:)`. I suppose we could extend that to work on instances too (`\myString.removeFirst(_:)`), but that would be a change from key paths.

> [@Lantua](#):
>
> `+` operators for example is leered upon with `+(_:, _:)` or `+(lhs:,rhs:)` with which this rule dosn't help.

Operators are actually an interesting question. I think we could justify keeping the existing behavior for them; they live in a separate namespace from properties, there's an ambiguity between prefix and postfix operators that would need some additional design to resolve, and they're special in various other ways too.

---

<div class="post-metadata">

**Author:** ![Paul\_Cantrell](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/paul_cantrell/32/16764_2.png) [@Paul\_Cantrell](https://forums.swift.org/u/Paul_Cantrell)\
**Post date:** [July 17, 2019, 9:11pm UTC](https://forums.swift.org/t/require-parameter-names-when-referencing-to-functions/27048/22 "2019-07-17T21:11:37Z")

</div>

> [@beccadax](#):
>
> It is non-uniform, but there's no way around that without changing `(x:y:)` to something else, like `[x:y:]` . I don't think a change like that currently is, or should be, on the table.

I tend to agree. I just figure someone should at least ask the question. It’s something to mention in the proposal.

What about this one?

> [@Paul\_Cantrell](#):
>
> Do the problems Brent mentions about source compatibility breaks etc. not apply to no-args / nullary funcs? If not, why not?

I wouldn’t want to choose an approach that leaves a dangling problem for no-arg functions and requires yet another across-the-board change. Measure twice, cut once.

---

<div class="post-metadata">

**Author:** ![salutis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/salutis/32/1156_2.png) [@salutis](https://forums.swift.org/u/salutis)\
**Post date:** [July 18, 2019, 6:52am UTC](https://forums.swift.org/t/require-parameter-names-when-referencing-to-functions/27048/23 "2019-07-18T06:52:47Z")

</div>

+1 for the parameter names being always required, including init(\_:) and similar. These names play a significant semantic role in Swift and so it is very important for the programmer to always see them.

---

<div class="post-metadata">

**Author:** ![jawbroken](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@jawbroken](https://forums.swift.org/u/jawbroken)\
**Post date:** [July 18, 2019, 12:56pm UTC](https://forums.swift.org/t/require-parameter-names-when-referencing-to-functions/27048/24 "2019-07-18T12:56:43Z")

</div>

> [@beccadax](#):
>
> I think the idea of the keypath syntax for methods was that it would get some representation of an unbound instance method from a type, e.g. `\String.removeFirst(_:)` . I suppose we could extend that to work on instances too ( `\myString.removeFirst(_:)` ), but that would be a change from key paths.

Maybe I've done too much metaprogramming in my life but I don't see that much difference between metatypes and types here.

---

<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:** [July 18, 2019, 6:24pm UTC](https://forums.swift.org/t/require-parameter-names-when-referencing-to-functions/27048/25 "2019-07-18T18:24:31Z")

</div>

Thinking through some of the consequences of this pitch, I find it detrimental to code for both readers and writers.

Simple calls like:

```swift
let c = zip(a, b).map(max)

```

would become:

```swift
let c = zip(a, b).map(max(_:_:))

```

where the extraneous symbols after `max` make it more mentally taxing to remember how to write, and more challenging to read.

This change would make the Swift language objectively worse, and I am strongly opposed to it.

---

<div class="post-metadata">

**Author:** ![beccadax](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/beccadax/32/34217_2.png) [@beccadax](https://forums.swift.org/u/beccadax)\
**Post date:** [July 18, 2019, 6:52pm UTC](https://forums.swift.org/t/require-parameter-names-when-referencing-to-functions/27048/26 "2019-07-18T18:52:29Z")

</div>

> [@jawbroken](#):
>
> Maybe I've done too much metaprogramming in my life but I don't see that much difference between metatypes and types here.

Currently, `\` reaches "down" a level—you use the type to form an instance key path, you (would) use the metatype to form a type key path, etc. `\myString.removeFirst(_:)` would muddy that rule.

---

<div class="post-metadata">

**Author:** ![pyrtsa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pyrtsa/32/740_2.png) [@pyrtsa](https://forums.swift.org/u/pyrtsa)\
**Post date:** [July 18, 2019, 7:12pm UTC](https://forums.swift.org/t/require-parameter-names-when-referencing-to-functions/27048/27 "2019-07-18T19:12:44Z")

</div>

Just thinking out aloud, would there be a trade-off worth making to still allow shorthand identifiers like `max` (and, by extension, all operators) to refer to functions that take all their arguments without names?

That would incur some type checker overhead (having to consider the variants of form `max()`, `max(_:)`, `max(_:_:)`, etc), but much less than currently.

---

<div class="post-metadata">

**Author:** ![clayellis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/clayellis/32/43646_2.png) [@clayellis](https://forums.swift.org/u/clayellis)\
**Post date:** [July 18, 2019, 7:47pm UTC](https://forums.swift.org/t/require-parameter-names-when-referencing-to-functions/27048/28 "2019-07-18T19:47:24Z")

</div>

> [@Nevin](#):
>
> Thinking through some of the consequences of this pitch, I find it detrimental to code for both readers and writers.

I can't agree more.

I absolutely understand the motivation here, but it comes at great cost to readability/writability. (Too great a cost, IMO.)

---

<div class="post-metadata">

**Author:** ![ashin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ashin/32/2783_2.png) [@ashin](https://forums.swift.org/u/ashin)\
**Post date:** [July 18, 2019, 11:09pm UTC](https://forums.swift.org/t/require-parameter-names-when-referencing-to-functions/27048/29 "2019-07-18T23:09:03Z")

</div>

> [@Nevin](#):
>
> the extraneous symbols after `max` make it more mentally taxing to remember how to write, and more challenging to read.

I agree with you on the readability point, but I'd imagine autocomplete would take care of writing out the argument separators in most cases.

---

<div class="post-metadata">

**Author:** ![clayellis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/clayellis/32/43646_2.png) [@clayellis](https://forums.swift.org/u/clayellis)\
**Post date:** [July 19, 2019, 5:18am UTC](https://forums.swift.org/t/require-parameter-names-when-referencing-to-functions/27048/30 "2019-07-19T05:18:43Z")

</div>

> [@ashin](#):
>
> I'd imagine autocomplete would take care of writing out the argument separators in most cases.

Sure, but you write the code once and read it many more times thereafter — as such, more weight should be given to readability.

---

<div class="post-metadata">

**Author:** ![Pranshugoyal](https://avatars.discourse-cdn.com/v4/letter/p/b5e925/32.png) [@Pranshugoyal](https://forums.swift.org/u/Pranshugoyal)\
**Post date:** [July 19, 2019, 6:16am UTC](https://forums.swift.org/t/require-parameter-names-when-referencing-to-functions/27048/31 "2019-07-19T06:16:41Z")

</div>

Can it be done so that providing parameter names is optional? i.e. when compiler is unable to resolve the function it simply throws an error saying "unable to resolve, provide complete name with parameters"?

---

<div class="post-metadata">

**Author:** ![QuinceyMorris](https://avatars.discourse-cdn.com/v4/letter/q/d26b3c/32.png) [@QuinceyMorris](https://forums.swift.org/u/QuinceyMorris)\
**Post date:** [July 19, 2019, 6:32am UTC](https://forums.swift.org/t/require-parameter-names-when-referencing-to-functions/27048/32 "2019-07-19T06:32:37Z")

</div>

I would suggest, without any technical backing behind this idea, that we "just" eliminate type inference as way of resolving whether names refer to functions, and keep only syntactic resolution.

For example, if there is (1) a non-function declaration for `foo` and (2) a function declaration for `foo` (with any combination of parameters), then writing a reference to `foo` would unambiguously refer to #1. If there is no non-function declaration, but only one function declaration, `foo` would refer to that function. If there is more than one function declaration (with different parameters), the parameter keywords would be needed to disambiguate.

The point is that type inference doesn't come into that, so the combinatorial explosion problem shouldn't occur. (Right??)

One of the great things about Swift is that you don't have to write something if you know that Swift knows what you mean. Forcing parameter names to **always** be written out forces you to write them even when there's only one possible thing they could mean. I'd hate for that to happen. (And I agree with @Nevin's point about readability.)

---

<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:** [July 19, 2019, 7:51am UTC](https://forums.swift.org/t/require-parameter-names-when-referencing-to-functions/27048/33 "2019-07-19T07:51:50Z")

</div>

Why would we need to require `init(_:)` or `max(_:_:)`? This would be pain in the butt and hurt readability, not even speaking about how many code points would be required to be migrated / rewritten.

I view the solution from a more flexible angle. Only require parameter names iff there is at least one label that would be critical for the resolution path or if you need explicitly provide an information about how much parameters your function overload has that you wish the compiler to pick from.

That way the above can stay as `init` and `max` unless you need the second part of the rule or would like to avoid the compiler from picking a labeled overload, only then you may want to use `init(_:)` or `max(_:_:)`.

---

<div class="post-metadata">

**Author:** ![toph42](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/toph42/32/7807_2.png) [@toph42](https://forums.swift.org/u/toph42)\
**Post date:** [July 19, 2019, 1:13pm UTC](https://forums.swift.org/t/require-parameter-names-when-referencing-to-functions/27048/34 "2019-07-19T13:13:57Z")

</div>

I oppose any implementation that forces reference to `init(_:)` or `max(_:_:)` but support an option to disambiguate when _needed._

---

<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:** [July 19, 2019, 1:55pm UTC](https://forums.swift.org/t/require-parameter-names-when-referencing-to-functions/27048/35 "2019-07-19T13:55:19Z")

</div>

We could also allow the bare function name to refer to any functions which have _no_ parameter labels; that is, `foo` could reference `foo`, `foo(_:)`, `foo(_:_:)`, etc. without allowing it to reference `foo(bar:)`. That change would be slightly more breaking, but I think has more justifiable semantics. We don't currently allow typing as much of a property name as is required to disambiguate:

```swift
struct Baz {
    var foo: Int = 0
    var bar: Int = 1
}

print(Baz().f) // we only need the 'f' to know we're talking about foo!

```

I view this proposal as disallowing the same sort of construction for method/function names.

---

<div class="post-metadata">

**Author:** ![anon9791410](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@anon9791410](https://forums.swift.org/u/anon9791410)\
**Post date:** [July 19, 2019, 4:25pm UTC](https://forums.swift.org/t/require-parameter-names-when-referencing-to-functions/27048/36 "2019-07-19T16:25:54Z")

</div>

I don't like this proposal.

The source of the problem comes from a lack of standardization of naming. As it is, the standard library often uses the name of return values as method names – that is, the name of what the method would be if it were a property. (`first` is an example.) There will be edge cases, but generally, disallowing that eliminates the confusion; my general solution is to prefix all such methods with `get`.

Any language with functions that can be referred to with constants/variables needs to tackle this. It's not obvious until you come across your own related problem – like Soroush has!

I want the method. My proposal to get it in is to start a precedent for better naming. As a property, it's `count`. As a method, it should be `getCount`.

---

<div class="post-metadata">

**Author:** ![clayellis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/clayellis/32/43646_2.png) [@clayellis](https://forums.swift.org/u/clayellis)\
**Post date:** [July 19, 2019, 4:39pm UTC](https://forums.swift.org/t/require-parameter-names-when-referencing-to-functions/27048/37 "2019-07-19T16:39:25Z")

</div>

> [@QuinceyMorris](#):
>
> If there is more than one function declaration (with different parameters), the parameter keywords would be needed to disambiguate.

I like this approach — disambiguate as needed.

---

<div class="post-metadata">

**Author:** ![anon9791410](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@anon9791410](https://forums.swift.org/u/anon9791410)\
**Post date:** [July 19, 2019, 4:59pm UTC](https://forums.swift.org/t/require-parameter-names-when-referencing-to-functions/27048/38 "2019-07-19T16:59:16Z")

</div>

Although `expectation` is named incorrectly (it is more accurately `makeExpectation`), I don't think it this is a problem in practice, because we make an expectation using the XCTestCase instance:

```swift
let expectation = self.expectation(description: "")

```

And while better naming would eliminate `self` for common usage, we'd still need to disambiguate using what Soroush is proposing, if we cared to store the method, and there were overloads.

```swift
extension XCTestCase {
  func makeExpectation(description: String) -> XCTestExpectation {
    expectation(description: description)
  }

  func makeExpectation(
    for predicate: NSPredicate,
    evaluatedWith object: Any?,
    handler: XCTNSPredicateExpectation.Handler? = nil
  ) -> XCTestExpectation {
    expectation(
      for: predicate,
      evaluatedWith: object,
      handler: handler
    )
  }
}

final class TestCase: XCTestCase {
  func test() {
    let expectation = makeExpectation(description: "")
    let makeExpectation = makeExpectation(description:)
  }
}

```

It's because of cases where overload disambiguation isn't needed, that I'm against this proposal.

```swift
let makeExpectation: (String) -> XCTestExpectation
makeExpectation = self.makeExpectation

```

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [July 19, 2019, 6:59pm UTC](https://forums.swift.org/t/require-parameter-names-when-referencing-to-functions/27048/39 "2019-07-19T18:59:23Z")

</div>

I'm a big proponent of this ([clarity at the point of use, even over brevity](https://swift.org/documentation/api-design-guidelines/#fundamentals)), but I think people have made really good points about it flipping from helpful to noise for functions with unlabeled arguments, like operators or `max` or even converting initializers. The danger in those cases is around overloading based on type, and that's always going to be a concern for Swift.

I'm against the "disambiguate as needed" approach because it makes it harder for library authors to change their code. If a method has a sensible base name, and you want to add functionality that would share the same base name but uses different argument labels, you shouldn't have to worry about whether someone might be referring to the method with just the base name. I think a lot of the reason this feels weird is because argument labels aren't considered part of a method's name in many other languages, but we _do_ think they are in Swift and have been moving the compiler in that direction [for a long time](https://forums.swift.org/t/review-se-0111-remove-type-system-significance-of-function-argument-labels/3209/41).

(I'm also against the "methods and properties should not have overlapping names" suggestion, but I think that's only one of the motivations here.)

EDIT: I forgot to post my conclusion: "so I support the middle ground where `foo` can refer to `foo()`, `foo(_:)`, `foo(_:_:)`, etc., but not to `foo(bar:)` or `foo(_:baz:)`".

DOUBLE EDIT: …even though, ugh, this is going to be annoying to implement.

---

<div class="post-metadata">

**Author:** ![griotspeak](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/griotspeak/32/1966_2.png) [@griotspeak](https://forums.swift.org/u/griotspeak)\
**Post date:** [July 19, 2019, 7:04pm UTC](https://forums.swift.org/t/require-parameter-names-when-referencing-to-functions/27048/40 "2019-07-19T19:04:28Z")

</div>

What if we stop trying to do the 'fully disambiguated' case inline? Let's make a syntax for getting a fully qualified and unambiguous function/method that isn't necessarily meant to be used inside a call to `map` but can be used before and yields a value that you can pass to `map`

[Previous page](https://forums.swift.org/t/require-parameter-names-when-referencing-to-functions/27048.md?page=1)

[Next page](https://forums.swift.org/t/require-parameter-names-when-referencing-to-functions/27048.md?page=3)
