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

Constructor signature rule (analogous to function signature rule) #2090

Closed
terminalnode opened this issue Jun 26, 2023 · 3 comments
Closed
Assignees
Milestone

Comments

@terminalnode
Copy link

Expected Rule behavior

There is an experimental function signature rule which is fantastic. I was first going to file a bug report that this is not applied to constructors, but on closer inspection of the different cases I think perhaps constructor validation should be a different rule.

This config enabled both function signature and the proposed constructor signature:

[*.{kt,kts}]
ktlint_standard_function-signature = enabled
ktlint_standard_constructor-signature = enabled
ktlint_function_signature_rule_force_multiline_when_parameter_count_greater_or_equal_than = 2

They both use the same force multiline rule for consistency.

The most clear-cut case is for secondary constructors:

// Allowed
data class TestGood(val a: Int) {
  // Parameter count < 2, multiline allowed but not enforced
  constructor(a: String) : this(a.toInt())

  // Parameter count < 2, multiline allowed but not enforced
  constructor(
    a: Long,
  ) : this(a.toInt())

  // Multiline enforced
  constructor(
    a: Int,
    b: Int,
  ) : this(a + b)
}

// Disallowed
data class TestBad(val a: Int) {
  constructor(a: Int, b: Int) : this(a + b)
}

Then there is the case of primary constructors. These are up to debate, but IMO they should be treated the same way as secondary constructors.

// Passes - parameter count < 2, multiline allowed but not enforced
data class TestGood1(val a: Int)

// Passes - parameter count < 2, multiline allowed but not enforced
data class TestGood2(
  val a: Int,
)

// Passes - parameter count ≥ 2, multiline enforced
data class TestGood3(
  val a: Int,
  val b: Int,
)

// Fails - parameter count ≥ 2, multiline enforced
data class TestBad(val a: Int, val b: Int)
@paul-dingemans
Copy link
Collaborator

There is an experimental function signature rule which is fantastic. I was first going to file a bug report that this is not applied to constructors, but on closer inspection of the different cases I think perhaps constructor validation should be a different rule.

I am very pleased that you like the function-signature rule. It is also one of my favorite rules. Your examples should be taken into account in the scope of #1349 which was already planned for the 1.0 release.

@paul-dingemans
Copy link
Collaborator

Implemented in #1349

@paul-dingemans
Copy link
Collaborator

See #2119

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants