Named-only parameters #442
Replies: 11 comments 61 replies
-
Hello! It quite nice that you considered Swift in the KEEP, but you didn't elaborate on why you decided to go with That does have a lot of cases, so we can write functions like this: fun String.write(to file: File) {
// to is inaccessible
// file is accessible
}
fun main() {
"test".write(to = File("..."))
} Maybe not the best example, but I hope it gives the idea Combining with the previous ideaWe also could introduce a new type of modifier that will not require to duplicate a variable name when we just want it named: fun String.reformat(
named normalizeCase: Boolean,
named(uppercase) uppercaseFirstLetter: Boolean,
) |
Beta Was this translation helpful? Give feedback.
-
The KEEP has a quite thorough technical part, but the motivation does not seem to be robust. There is not information about how this feature interferes with other existing and planned features. I don't see a lot of added value in this proposal. But it complicates the language. So without understanding how this feature will work with other similar features, I do not like it. In my opinion, any new lanaugage feature should solve more than one problem, unless it is a very important problem. |
Beta Was this translation helpful? Give feedback.
-
Note on positional data classes: When migrating |
Beta Was this translation helpful? Give feedback.
-
👏 I find the proposed solution and its motivation convincing, though I had previously been exposed for quite some time to Python's The only thing I'm not sure about: Whether Swift's default naming approach would have been preferable. But you're possibly right at hinting that this ship might have sailed. |
Beta Was this translation helpful? Give feedback.
-
Seems that this feature can be covered by annotation that would be applied to entire constructor/function and individual parameters. Plus compiler warning, that can be as well configured to report with any level: ERROR, WARN. Also, this might be start of a bigger story, integration more lint rules directly into compiler, or via plugins. |
Beta Was this translation helpful? Give feedback.
-
It seems that another missing use-case for the feature is keeping source compatibility when renaming, reordering, and adding parameters. It is useful for the end users, as they don't need to worry about the binary compatibility. |
Beta Was this translation helpful? Give feedback.
-
Personally I don't see much need for this feature, but since it's trivial to explain to new users ("it's a parameter that must be named on the callsite") and doesn't seem to have tricky edge cases, I guess it's fine to add. Plus, it eliminates the current hack fun foo(
normal: Foo,
_n: NamedOnly = NamedOnlyImpl,
thisParameterIsNamedOnly: Bar
) {
}
sealed class NamedOnly
private object NamedOnlyImpl : NamedOnly() which is welcome (although thankfully few people use it, but it's mentioned every once in a while in the forums). However, I'm a bit worried that library authors will overuse this and make code more complex than it needs to be. For example, the proposal mentions examples of functions where named arguments should be used, including In my own codebases, I very often see code like assertEquals(4, users.counter.get())
assertEquals("Daniel", users.toList().last()) The proposal is implying that this code would be more readable as: assertEquals(expected = 4, users.counter.get())
assertEquals(expected = "Daniel", users.toList().last()) And that listOf("a", "b", "c")
.joinToString(", ") would definitely be more readable as: listOf("a", "b", "c")
.joinToString(separator = ", ") I disagree with both of these takes. I think this is applying this feature too far, and the current implementation is perfectly reasonable. Now, should IntelliJ offer a quick-fix to explicitly add the named arguments for these examples? Maybe. But I don't think that code should be forbidden to write. |
Beta Was this translation helpful? Give feedback.
-
In the section "Multiple lambda arguments", please clarify what this sentence means:
How does |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I'm afraid this kind of feature opens the door to a new wave of requests: Please add Is it really necessary to define in the function interface how parameters should be passed? |
Beta Was this translation helpful? Give feedback.
-
Really?
A method with 10 (ten) positional parameters, it is OK for me. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
This is a discussion of the "Named-only parameters" feature. The text of the proposal can be found here.
We introduce a new modifier
named
on function value parameters that obliges callers of this function to specify the name of this parameter.This enhances code readability and reduces errors caused by value associated with the wrong parameter when passed in positional form.
Here's an example:
Beta Was this translation helpful? Give feedback.
All reactions