does `.let` check for uninitialized too?
# announcements
n
does
.let
check for uninitialized too?
k
let
doesn't really do anything, it's just a normal extension function.
x
and
x.let
both just evaluate
x
first, and that does check for
lateinit
.
n
so if i get this:
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property _presenter has not been initialized
whats the best way to check it if its initialized?
k
Normally you don't, if you have to check you better just make it nullable. However, there's also `::x.isInitialized`: https://kotlinlang.org/docs/reference/properties.html#checking-whether-a-lateinit-var-is-initialized-since-12
1
n
thanks
r
Yeah you should not check if it’s initialized. It’s proof you’re doing something wrong
1
n
so if i get to a point that i need to check ifInitialized its better to change the
lateinit
to var and assign it with null first?
☝️ 1
k
Hopefully it was already a
var
😉. But most of the time yes, you gain the type safety you lost with
lateinit
back.
n
lol, yeah, surly it was
var
i meant for the null part
thanks guys
r
You should use
lateinit
when you know that the variable will never be
null
, for example when the value is always set, but outside of the constructor, like with injection or in Android’s
onCreate