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?