Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trailing comma: new feature from Kotlin 1.4 #419

Closed
orchestr7 opened this issue Oct 18, 2020 · 4 comments · Fixed by #743
Closed

Trailing comma: new feature from Kotlin 1.4 #419

orchestr7 opened this issue Oct 18, 2020 · 4 comments · Fixed by #743
Assignees
Labels
documentation Improvements or additions to documentation epic Major feature, could be decomposed into smaller tasks

Comments

@orchestr7
Copy link
Member

orchestr7 commented Oct 18, 2020

Kotlin 1.4 introduce the trailing comma feature. So we need to support it now not only for ENUMs as we did it before, but our code-style should force people to do this. For example:

fun foo(
    a: Int,
    b: String, // <--- this trailing comma is extremely useful for merging conflicts and swapping easily line without changing them
) 

val colors = listOf(
    "red",
    "green",
    "blue", //trailing comma
)

same should be done for function calls/constructor arguments/e.t.c

Kotlin supports trailing commas in the following cases:

Enumerations
Value arguments
Class properties and parameters
Function value parameters
Parameters with optional type (including setters)
Indexing suffix
Lambda parameters
when entry
Collection literals (in annotations)
Type arguments
Type parameters
Destructuring declarations

@orchestr7 orchestr7 self-assigned this Oct 18, 2020
@orchestr7 orchestr7 added documentation Improvements or additions to documentation epic Major feature, could be decomposed into smaller tasks labels Oct 18, 2020
@petertrr
Copy link
Member

It should be pretty easy to implement from the rules side, but the most important question here is: how should user specify language version? We should either support this setting in our plugin(s) or yml config, or once again consider moving from ktlint to detekt as backend, because detekt already supports such settings.

@orchestr7
Copy link
Member Author

please have a look at #728 first

@petertrr
Copy link
Member

petertrr commented Feb 2, 2021

@kentr0w implement it like this: if there is a newline after the last element in parameter/argument list, then there should be a comma after it.
Obviousely, check that kotlin version is >= 1.4

@petertrr
Copy link
Member

petertrr commented Feb 2, 2021

@kentr0w please add it like this in the code style:

Since kotlin 1.4 you can use trailing comma in most list structures, including value argument lists, type parameter lists, parameters in indexing etc. Usage of trailing comma is recommended for long lists since it makes merging and resolving merge conflicts really seamless. If you use trailing comma, you should also put a line break after it.

Invalid code:
fun foo(
    a: Int,
    b: Int) {}

Valid code:
fun foo(
    a: Int,
    b: Int,
) {}

@kentr0w kentr0w mentioned this issue Feb 2, 2021
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation epic Major feature, could be decomposed into smaller tasks
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants