This code compiles fine despite of the property is not initialized.
class Test {
var myProperty: Int // OK
set(value) {}
constructor() {
myProperty = 42
}
}
I get a suggestion from IDE to convert this constructor to primary constructor. But when I do so, I get the error message.
class Test() {
var myProperty: Int // Error: Property must be initialized
set(value) {}
init {
myProperty = 42
}
}
But what is more strange is that after adding an empty constructor the code compiles again.
class Test {
constructor()
var myProperty: Int // OK
set(value) {}
init {
myProperty = 42
}
}
Why does the compiler behaves differently depending on the way constructor is declared? I expect that code fragments above either all compile or none of them compile at all.
Iām using Kotlin version 1.2.31