Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

249
Visualizações
Is it possible to use a private secondary constructor to assign a val in Kotlin?

I am trying to reduce code repetition by using a private constructor to assign a class field, but this does not seem to be possible.

What I'm trying to do is the exemplified here:

class Foo {
    private val bar: Int
    private val baz: Int

    constructor(bar: Int, baz: Int) : this(baz) {
        this.bar = bar
    }

    constructor(bar: String, baz: Int) : this(baz) {
        this.bar = bar.toInt()
    }

    private constructor(baz: Int) {
        this.baz = baz
    }
}

The alternative, that works, but which I am not satisfied with is doing the following:

class Foo {
    private val bar: Int
    private val baz: Int

    constructor(bar: Int, baz: Int) {
        this.bar = bar
        this.baz = baz
    }

    constructor(bar: String, baz: Int) {
        this.bar = bar.toInt()
        this.baz = baz
    }
}

To be clear I am not satisfied because of the replication of the this.baz assignment.

Is this simply not possible in Kotlin, or am I missing something?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You should rewrite this as:

class Foo(
    private val bar: Int,
    private val baz: Int, 
) {
    constructor(bar: String, baz: Int) : this(bar.toInt(), baz)
}

The (Int, Int) constructor has been made into a primary constructor, initialising all the values. And the second constructor delegates the primary constructor.

The first two secondary constructors that you declared in your non-working code cannot reassign the vals, because they delegate to another constructor, and it is assumed that that constructor initialises all the necessary properties. Though in this case, it doesn't, so you get another error.

over 4 years ago · Santiago Trujillo Relatório

0

That can't work because your private constructor fails to initialize all properties. You're treating it like a simple function call.

Why not do this?

class Foo {
    private val bar: Int
    private val baz: Int

    constructor(bar: Int, baz: Int) { 
        this.bar = bar
        this.baz = baz
    }

    constructor(bar: String, baz: Int) : this(bar.toInt(), baz) 
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda