Hi Kotliner,
I am getting very confused with some unpredictable compiler behaviors when to val/var in constructor and when not to use them, why such differences and benefits or such rules.
I am using almost latest kotlin version (1.2.41)
Please note, there’s no runtime differences or impact but its only related to warning/suggestion by compiler
Example : the difference is only using value inside a function or using value outside a function
//Suggesting me to remove val from constructor
class TestClass1(value: Boolean = false)
{
val output = if(value) "value is true" else "value is false"
}
//val/var is mandatory and suggesting me to make it private
class TestClass2(private val value: Boolean = false)
{
fun output() = if(value) "value is true" else "value is false"
}