What is the relative performance of a `lateinit va...
# announcements
f
What is the relative performance of a `lateinit var`'s
isInitialized
? Is it just checking some flag, or is it some expensive reflective operation, maybe it allocates objects?
d
It's intrinsified to a simple null check.
lateinit var foo: T
becomes
var foo: T? = null
and
isInitialized
simply checks
foo != null
f
thanks
p
For stuff like this just check the bytecode and re-compile it to java if you don't understand bytecode well enough
So if you take this code snippet:
Copy code
lateinit var variable: String

fun main() {
  val initialized = ::variable.isInitialized
}
y
👌 1
p
You can just do it in android studio
Find action -> bytecode
Copy code
public static final void main() {
      boolean initialized = variable != null;
   }
l
I'd like to point out that there are very few cases when you should use this. You would probably rather use a nullable type than not knowing if a variable was initialized
âž• 2
Don't make your code go
isInitialized
everywhere, or you'll return to a Java
if not null
boilerplate everywhere
💯 1
109 Views