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

Rule 6.1.3: Do not use the primary constructor if it is empty and has no sense #439

Closed
petertrr opened this issue Oct 23, 2020 · 0 comments · Fixed by #528
Closed

Rule 6.1.3: Do not use the primary constructor if it is empty and has no sense #439

petertrr opened this issue Oct 23, 2020 · 0 comments · Fixed by #528
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@petertrr
Copy link
Member

petertrr commented Oct 23, 2020

The primary constructor is part of the class header: it goes after the class name (and optional type parameters).
But in is useless - it can be omitted.

Bad example:

// simple case that does not need a primary constructor
class Test() {
    var a: Int = 0
    var b: Int = 0
}

// empty primary constructor is not needed here also
// it can be replaced with a primary contructor with one argument or removed
class Test() {
    var a  = "Property"

    init {
        println("some init")
    }

    constructor(a: String): this() {
        this.a = a
    }
}

Good example:

// the good example here is a data class, but this example shows that you should get rid of braces for primary constructor
class Test {
    var a: Int = 0
    var b: Int = 0
}
@petertrr petertrr added the enhancement New feature or request label Oct 23, 2020
@petertrr petertrr added this to the Chapter 6 milestone Oct 23, 2020
@kentr0w kentr0w self-assigned this Nov 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants