Jonathan Lennox
05/18/2023, 7:41 PMupdateFields
into the second constructor it does. Is there a way I can get Kotlin to know the fields are always initialized, without duplicating the code between updateFields
and the constructor?
class DemoClass
{
var field1: Int
private set
var field2: Int
private set
constructor(f1: Int, f2: Int) {
field1 = f1
field2 = f2
}
constructor(basef: Int) {
updateFields(basef)
}
fun updateFields(basef: Int) {
field1 = basef * 2
field2 = basef + 2
}
}
Landry Norris
05/18/2023, 7:50 PMHaram Kwon
05/18/2023, 7:50 PMclass DemoClass
{
var field1: Int = 0
private set
var field2: Int = 0
private set
constructor(f1: Int, f2: Int) {
field1 = f1
field2 = f2
}
constructor(basef: Int) {
updateFields(basef)
}
fun updateFields(basef: Int) {
field1 = basef * 2
field2 = basef + 2
}
}
Jonathan Lennox
05/18/2023, 7:51 PMLandry Norris
05/18/2023, 7:52 PMJonathan Lennox
05/18/2023, 7:53 PMHaram Kwon
05/18/2023, 8:00 PMephemient
05/19/2023, 12:59 AMclass DemoClass(field1: Int, field2: Int) {
var field1 = field1
private set
var field2 = field2
private set
constructor(basef: Int) : this(
field1 = basef * 2,
field2 = basef + 2,
)
}
Jonathan Lennox
05/19/2023, 4:05 AMupdateFields
method to be called later to change the values).ephemient
05/19/2023, 4:17 AMclass DemoClass(field1: Int, field2: Int) {
var field1 = field1
private set
var field2 = field2
private set
constructor(basef: Int) : this(0, 0) {
updateFields(basef)
}
}