mzgreen
10/29/2018, 11:30 AMclass Foo {
private val test: String
init {
test = "test"
}
}
and this doesn’t:
class Foo {
private val test: String
init {
bar()
}
private fun bar() {
test = "test"
}
}
Is there any valid case why it may fail? Or it’s the compiler’s fault that it doesn’t recognize bar
is called inside an init
?robin
10/29/2018, 11:46 AMinit
block because the compiler can statically check that, but it won't check function calls you do from init
blocks, because these could become arbitrarily complex and would probably slow down the compiler significantly.mzgreen
10/29/2018, 11:48 AMinline
. I would expect it to work in this case because it would be essentially the same as having this code inside a constructor, right?robin
10/29/2018, 11:52 AMrobin
10/29/2018, 11:52 AMmzgreen
10/29/2018, 11:56 AMinit
blocks. But I’m just curious what is the reason it doesn’t work. I know why calling methods from within a constructor can be bad so I expected to be a specific reason for this in Kotlin.Pavlo Liapota
10/29/2018, 12:28 PMtest
inside method as variable is declared as val
.robin
10/29/2018, 12:30 PMvar
.Pavlo Liapota
10/29/2018, 12:39 PMlateinit
can be used 🙂 Not a clean solution, but as you said “It’s just a tradeoff decision made.”
May be contract
will be added for this case in the future ¯\_(ツ)_/¯robin
10/29/2018, 12:57 PMPavlo Liapota
10/29/2018, 1:12 PMrobin
10/29/2018, 1:12 PM