you can only seperate declaration and initilizatio...
# announcements
t
you can only seperate declaration and initilization if the latter happens in a constructor or
init
block. in the end they wont be split this way. you can use
by lazy{}
with `val`s
v
Is there a way, using lateinit to ensure that the variable is set just once?
p
For a local variable compiler will make sure that variable is initialized only once and that it is not used before initialization and will show compiler error otherwise. For a property that is initialized in some method such check is impossible in a common case. As @thana mentioned, if you make initialization in a constructor, then compiler will be able to make a check, so
lateinit
will not be needed.
t
when using a
val
with the
lazy
delegate the property will be written once and only once
v
Only when it's accessed the first time. Awesome, thanks!
k
You could also write a simple delegate for this,
var foo by setOnce()
or something.