Is there a way to use the very handy `lateInitVar....
# announcements
h
Is there a way to use the very handy
lateInitVar.isInitialized()
from java?
r
Why would you use
isInitialized
?
v
To not get a
NullPointerException
if the variable was not initialized in time, but either handle it differently or throw a more meaningful error.
h
It actually doesn't throw an NPE, but an UninitializedPropertyAccessException. Simply making a wrapper function worked like a charm, the check is now available also from my java code.
r
Weird. If the property not having a value is a valid state, then it should be nullable. I think it’s a weird usage of
lateinit
here. I only use
isInitialized
when debugging
v
It can have valid use-cases, like it gets set by injection and is used by some event listening and the event listening can be triggered before the injection was done or similar
h
In my case it is set in an android activity in onCreate(), but being used by a java class in a place in the code that wasn't mine. Haven't touced this for a year, so it was easier to write a function wrapper and make a check before use than to track down all use and make the variable nullable.
r
Well, that is an invalid use case for me. Things initialized in
onCreate
should be nullable
k
isInitialized
is an intrinsic. It does not exist in the compiled code. Instead it’s translated by the compiler to
lateInitVar != null
.
h
Maybe so, but this simple wrapper did it for me:
fun lateInitVarInitialized() = ::lateInitVar.isInitialized
k
Sure, but why taking the extra step of introducing a new function, if you could simply perform a null check?
v
@ribesg setting in
onCreate
sounds to me like the typical injection use-case for which
lateinit
vars are intended. Might be my lack of Android dev knowledge, but why do you think it is not appropriate and what do you think it is appropriate for instead?
r
It’s just that you can reference the instance and call stuff on it before
onCreate
is called on it. It’s not like when there’s something like a deserializer which calls an empty constructor and sets
lateinit
fields right away. It’s easy to write
myActivity.x
without thinking that at this point
onCreate()
hasn’t been called, so I prefer to have to write
val x = myActivity?.x ?: error("…")
. Just writing
myActivity.x
and seeing the error makes me think about it.
k
Ah, I see. You’re accessing the getter, which will throw an exception if the value is null. Therefor you can’t check for null.
v
It’s just that you can reference the instance and call stuff on it before 
onCreate
 is called on it.
Oh, really? Android has a strange API imho then. From the name I would have expected it to be like a
@PostCreate
method in Spring or `@Inject`ed field in CDI which are done right after instantiation and before you get it for further operations.
🙃 1
c
If I remember correctly,
onCreate
is called when the activity is displayed to the user (or just a bit before that) which is not necessarily at the same time as object instantiation 🤔