https://kotlinlang.org logo
#language-proposals
Title
# language-proposals
i

igor.wojda

01/27/2017, 11:17 AM
Is there any particular technical reason why it is not possible to use
lateinit
with immutable (val) properties or is it just deliberty forced limitation? I wonder becouse this limitation does not allow to define non nullable variable that will be initialized latter only once. Now we can only have mutable injected variables witch can be latter changed by accident (val + lateinit would solve this issue) Now:
@Inject lateinit var repository: Repository
Want:
@Inject lateinit val repository: Repository
//initialization is delayed but it is still possible assign value only once
r

robin

01/27/2017, 11:46 AM
igor.wojda: A val has to be initialized directly, or in a place where the compiler can infer that it's impossible the val will be already initialized beforehand. A lateinit variable typically does exactly the opposite - it's initialized in a place where the compiler can NOT deduce that it will happen before it is first accessed. You might want to work around that with the lazy delegate.
2 Views