# Allow omitting argument values when variable present with same name as label

**URL:** <https://forums.swift.org/t/allow-omitting-argument-values-when-variable-present-with-same-name-as-label/58234>\
**Category:** Pitches\
**Created:** [June 17, 2022, 10:05pm UTC](https://forums.swift.org/t/allow-omitting-argument-values-when-variable-present-with-same-name-as-label/58234 "2022-06-17T22:05:14Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![mvolkmann](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mvolkmann/32/23891_2.png) [@mvolkmann](https://forums.swift.org/u/mvolkmann)\
**Post date:** [June 17, 2022, 10:05pm UTC](https://forums.swift.org/t/allow-omitting-argument-values-when-variable-present-with-same-name-as-label/58234/1 "2022-06-17T22:05:14Z")

</div>

Frequently in my code I calls like this where there are in-scope variables named color, size, and temperature and I cannot change someMethod to take positional arguments:

someMethod(color: color, size: size, temperature: temperature)

I would love to eliminate the repetition, perhaps like this:

someMethod(color:, size:, temperature:)

If it is problematic for colors to appear here, maybe another character could be used like:

someMethod(color\*, size\*, temperature\*)

---

<div class="post-metadata">

**Author:** ![0x41c](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/0x41c/32/23906_2.png) [@0x41c](https://forums.swift.org/u/0x41c)\
**Post date:** [June 17, 2022, 10:13pm UTC](https://forums.swift.org/t/allow-omitting-argument-values-when-variable-present-with-same-name-as-label/58234/2 "2022-06-17T22:13:43Z")

</div>

This seems like an interesting idea, but have you considered using underscores before the function identifiers to eliminate the need for any naming when passing in the arguments to the function? ex:

```swift
func someFunction(_ color: Color, _ size: CGSize, _ temperature: Double) {}

// Call the function without passing argument names
// Assume "color", "size", and "temperature" are already defined
func someFunction(color, size, temperature)

```

Those characters (the star and the colon) already have uses though, and shouldn't be re-used in such a context either.

---

<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:** [June 17, 2022, 10:25pm UTC](https://forums.swift.org/t/allow-omitting-argument-values-when-variable-present-with-same-name-as-label/58234/3 "2022-06-17T22:25:42Z")

</div>

The thing is, in most argument lists it's poor Swift style to give a parameter a label that would also be a good variable name. For instance, your function’s `color`, `size`, and `temperature` labels probably duplicate information available from the types of those parameters, which means they’re redundant and you shouldn’t use them. A more plausible example might look like:

```swift
someFunction(with: color, jelloCubeOf: size, at: temperature)

```

But `with`, `jelloCubeOf`, and `at` are not plausible names for a variable of those types, so you wouldn’t be able to use that feature here. And that isn’t a contrived example; the [API Design Guidelines](https://www.swift.org/documentation/api-design-guidelines/) call for you to "prefer method and function names that make use sites form grammatical English phrases”, and arguments that are prefixed by nouns are not usually going to form grammatical English phrases.

There _are_ a couple of cases where it’s idiomatic to use argument labels that might work as variable names—optional arguments, arguments to initializers and factory methods, etc.—but these aren’t super-common, and I’m not convinced it’s worth adding a feature for them.

---

<div class="post-metadata">

**Author:** ![mvolkmann](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mvolkmann/32/23891_2.png) [@mvolkmann](https://forums.swift.org/u/mvolkmann)\
**Post date:** [June 17, 2022, 10:43pm UTC](https://forums.swift.org/t/allow-omitting-argument-values-when-variable-present-with-same-name-as-label/58234/4 "2022-06-17T22:43:12Z")

</div>

Yes, I know I can use understands in the function definitions to make the arguments positional, but typically I'm calling library functions that use named arguments.

---

<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:** [June 17, 2022, 10:43pm UTC](https://forums.swift.org/t/allow-omitting-argument-values-when-variable-present-with-same-name-as-label/58234/5 "2022-06-17T22:43:34Z")

</div>

Although I don't think the OP's idea is worth pursuing, because, although this sort of redundancy is common, it's not _so common_ as to warrant a new feature, I also don't think the original intention of the following actually ended up looking as readable as intended:

> [@beccadax](#):
>
> The thing is, in most argument lists it's poor Swift style to give a parameter a label that would also be a good variable name.

Main reason: `.init`. But literals cause readability problems too.

```swift
someFunction(with: .init(gray: 0.5), jelloCubeOf: [1, 6], at: 33.2)

```

I never thought the bare prepositions were a good idea, and six years after the biggest change, I'm still not convinced. The separation of argument labels and parameter names is good, but primarily for removing prepositions from parameters, when the argument labels already include the direct objects.

---

<div class="post-metadata">

**Author:** ![mvolkmann](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mvolkmann/32/23891_2.png) [@mvolkmann](https://forums.swift.org/u/mvolkmann)\
**Post date:** [June 17, 2022, 10:46pm UTC](https://forums.swift.org/t/allow-omitting-argument-values-when-variable-present-with-same-name-as-label/58234/6 "2022-06-17T22:46:24Z")

</div>

Initializers are probably the most common place I run into this duplication. For example:

```swift
let person = Person(firstName: firstName, lastName: lastName, birthday: birthday)

```

---

<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:** [June 17, 2022, 10:59pm UTC](https://forums.swift.org/t/allow-omitting-argument-values-when-variable-present-with-same-name-as-label/58234/7 "2022-06-17T22:59:22Z")

</div>

That sounds like a use case for the ever-missing feature of applying functions, [`Optional.map`](https://developer.apple.com/documentation/swift/optional/map(_:)-7txtz)-style, to everything. E.g.

```swift
let person = (firstName, lastName, birthday)…Person.init

```

```swift
infix operator …

///- Remark: Hold option, press ;
@discardableResult
public func … <Input, Transformed>(
  input: Input,
  transform: (Input) -> Transformed
) -> Transformed {
  transform(input)
}

```

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [June 17, 2022, 11:47pm UTC](https://forums.swift.org/t/allow-omitting-argument-values-when-variable-present-with-same-name-as-label/58234/8 "2022-06-17T23:47:19Z")

</div>

Possible solution would be to have a third case (optional parameter names) in addition to the two we already have: empty/external argument name (required parameter name) and "\_" (no parameter name):

```swift
// not in Swift, straw-man syntax:
func someMethod(color _: Color, size _: Size, temperature _: Temperature) {}
// or this:
func someMethod( __color: Color,__ size: Size, __ temperature: Temperature) {}
// or this:
func someMethod(~ color: Color, ~ size: Size, ~ temperature: Temperature) {}

// usage - can use either with or without parameter name:
someMethod(color: color, size: size, temperature: temperature) // ok
someMethod(color, size, temperature) // ok
someMethod(color: color, size, temperature) // still ok

```

---

<div class="post-metadata">

**Author:** ![mayoff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mayoff/32/613_2.png) [@mayoff](https://forums.swift.org/u/mayoff)\
**Post date:** [June 18, 2022, 1:17am UTC](https://forums.swift.org/t/allow-omitting-argument-values-when-variable-present-with-same-name-as-label/58234/9 "2022-06-18T01:17:27Z")

</div>

> [@mvolkmann](#):
>
> `let person = Person(firstName: firstName, lastName: lastName, birthday: birthday)`

This is a case where the duplication is helpful, because if `firstName` and `lastName` are the same type (probably `String`), it would be easy to mix them up without the labels, and with the labels, a glance is enough to notice if they're backward.

Your original example (where all the arguments are different types) is a case where I'd consider omitting the labels, because there's no possibility of mixing up the argument order. Or I might use a preposition to label the first argument and omit the labels of the rest.

---

<div class="post-metadata">

**Author:** ![intswift](https://avatars.discourse-cdn.com/v4/letter/i/7cd45c/32.png) [@intswift](https://forums.swift.org/u/intswift)\
**Post date:** [June 18, 2022, 8:18am UTC](https://forums.swift.org/t/allow-omitting-argument-values-when-variable-present-with-same-name-as-label/58234/10 "2022-06-18T08:18:40Z")

</div>

What if the underscore were used in a reversed sense? For example, at the call site:

```swift
someMethod(color: _, size: _, temperature: _)

```

and the compiler will substitute the underscore with whatever variable has the same name as the argument name. I think the refactoring tool would need extra logic to recognise this if you tried to rename a variable using it.

---

<div class="post-metadata">

**Author:** ![mvolkmann](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mvolkmann/32/23891_2.png) [@mvolkmann](https://forums.swift.org/u/mvolkmann)\
**Post date:** [June 18, 2022, 9:45am UTC](https://forums.swift.org/t/allow-omitting-argument-values-when-variable-present-with-same-name-as-label/58234/11 "2022-06-18T09:45:59Z")

</div>

There’s no issue with types because what is specified is a list of labels, not variables. It is saying “I have variables whose names match the labels, so use those as the values.”

---

<div class="post-metadata">

**Author:** ![masters3d](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@masters3d](https://forums.swift.org/u/masters3d)\
**Post date:** [June 19, 2022, 12:56am UTC](https://forums.swift.org/t/allow-omitting-argument-values-when-variable-present-with-same-name-as-label/58234/12 "2022-06-19T00:56:39Z")

</div>

This makes sense to me but it should be a feature of the editor not the compiler.

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [June 20, 2022, 8:40pm UTC](https://forums.swift.org/t/allow-omitting-argument-values-when-variable-present-with-same-name-as-label/58234/13 "2022-06-20T20:40:18Z")

</div>

> [@anon9791410](#):
>
> I never thought the bare prepositions were a good idea, and six years after the biggest change, I'm still not convinced. The separation of argument labels and parameter names is good, but primarily for removing prepositions from parameters, when the argument labels already include the direct objects.

> [@beccadax](#):
>
> And that isn’t a contrived example; the [API Design Guidelines](https://www.swift.org/documentation/api-design-guidelines/) call for you to "prefer method and function names that make use sites form grammatical English phrases”, and arguments that are prefixed by nouns are not usually going to form grammatical English phrases.

i understand the motivation of that guideline, but in practice trying to make API calls read like english sentences ends up making really brittle APIs that change frequently, are hard to refactor, and run counter to what would be the most logical organization from an implementation standpoint.

programming complex systems is hard enough without trying to be a poet at the same time, 75% of the time the best argument label is what the name of the variable would be in the calling scope.

of the common prepositions, `as:` and `to:` are probably the only useful argument labels. i tend to have a hard time choosing between `of:` and `for:`, and i can’t remember many times where `by:` contributed any clarity at all.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [June 20, 2022, 11:46pm UTC](https://forums.swift.org/t/allow-omitting-argument-values-when-variable-present-with-same-name-as-label/58234/15 "2022-06-20T23:46:18Z")

</div>

_But it's obvious that `data(for:delegate:)` takes URL and `data(from:delegate:)` takes URLRequest... NOT!_

(and it's the other way around)

Besides it's still not "English". "data(for: x, delegating: y)" would be...

There's no universal consistency. Sometimes "forKey" is used even when there's no other "forXxx", other times function is overloaded and it's "from:" argument accepts different types. Then there are some serious æsthetic offenders: `CGAffineTransform(scaleX:y:)`

I'd echo the previous two posts admitting that while this is not "swifty", oftentimes I'd personally prefer seeing some less grammatically correct form... e.g. something as simple as struct's default member-wise initialiser rules that results into `Foo(bar:baz:)` for a struct with bar and baz fields.

---

<div class="post-metadata">

**Author:** ![zienag](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/zienag/32/43995_2.png) [@zienag](https://forums.swift.org/u/zienag)\
**Post date:** [June 21, 2022, 10:15pm UTC](https://forums.swift.org/t/allow-omitting-argument-values-when-variable-present-with-same-name-as-label/58234/16 "2022-06-21T22:15:27Z")

</div>

I think it’s worth mentioning that Scala has very similar feature called implicit parameters. It might be interesting to see how it’s going there.

[https://docs.scala-lang.org/tour/implicit-parameters.html](https://docs.scala-lang.org/tour/implicit-parameters.html)

---

<div class="post-metadata">

**Author:** ![austintatious](https://avatars.discourse-cdn.com/v4/letter/a/ce73a5/32.png) [@austintatious](https://forums.swift.org/u/austintatious)\
**Post date:** [June 22, 2022, 12:32am UTC](https://forums.swift.org/t/allow-omitting-argument-values-when-variable-present-with-same-name-as-label/58234/17 "2022-06-22T00:32:40Z")

</div>

I vote no on this on this for the simple reason that swift should remain as exception free as possible. There is very little gain here.
