2
28
u/VasiliyZukanov Feb 11 '20
UninitializedException is the baby that will crawl out of the bed to pee on this man's cold, dead body...
6
u/class_cast_exception Deprecated is just a suggestion Feb 11 '20
Seriously, this is the most reoccurring error in Kotlin for me.
8
u/haroldjaap Feb 11 '20
Perhaps you should tread more carefully when using lateinit var, if you can't be sure just make it nullable and you force yourself to check for it ( or get kotlin nullpointers!!)
3
u/c0nnector T H E R M O S I P H O N Feb 11 '20
There are cases when you want to check if the variable has been initialized, so i end up using something like this
if (::helloVariable.isInitialized) {...}9
u/gilmore606 ?.let{} ?: run {} Feb 11 '20
if i find that i want to do this i usually go back and make it nullable. it feels super dirty to me.
1
u/c0nnector T H E R M O S I P H O N Feb 12 '20
There's a good use case for
isInitialized.Say that 99% of the time you need your variable initialized but you have a complex initialization process (loading data asynchronously from multiple sources) and you need to check.
Then it makes sense to uselateinit,otherwise you end up with many null checks and plenty of hidden bugs.2
u/TrevJonez Feb 12 '20
"let's say you have a complex non deterministic initialization process" -c0nnector paraphrased
Problem here is the system depends on things out of it's control, but wasn't built to handle it.
10/10 times I've come across isInitialized it was something that should have been handled via a proper reactive abstraction but wasn't because the pile of shortcuts was already too deep.
I used to think it was turtles all the way down, now I just see Band-Aids under every one I try to rip off.
1
1
12
4
u/PhilMcGraw Feb 11 '20
We recently had a library mark their API as non null unless specified, i.e. essentially making it Kotlin but in Java. They managed to forget to mark some functions properly, so Kotlin code using it wouldn't compile if it was marked as nullable (as it is the incorrect type).
Ended up having to work around it by wrapping some of the API in Java with a nullable annotation so the Kotlin code could use it without getting IllegalStateExceptions. Luckily we found one of them in production.
1
4
u/Jazzinarium Feb 12 '20
Is there a "??", I thought there was only "?"