class Foo(val bar: Boolean,
val conditional: Int = if(bar) 1 else 2)
// works fine
fun test() {
val f = Foo(true)
}
// will not compile - unresolved reference 'bar'
fun test2() {
val f2 = Foo(bar = true, if(bar) 1 else 2)
}
// ugly workaround
fun test3() {
val bar = true
val f3 = Foo(bar, if(bar) 1 else 2)
}
Kotlin allows initializing fields with default values which can be based on previously initialized ones, but when you need to calculate property value in the runtime you need an external local variable. So I suggest to add ability to access initialized properties during construction. This example is synthetic but I often need this when data received from network and in protocol conditional read or skip for property value.