This message was deleted.
# feed
s
This message was deleted.
z
Why a separate
isInitialized
property instead of a null check? Is it to allow for initialization to a nullable value? (since
T
is allowed to be nullable)
s
It’s just so it covers some edge-cases. Theoretically, when the variable is lazily initialized, the initializer might do something and set variable to
null
. And then that initialization would happen every other time we reference it, since the value will again be
null
. This won’t happen if used correctly but with boolean we keep the implementation very precise. If you check the Kotlin source, they don’t use
isInitialized
but the property value is defined as
Any
and it holds some predefined object (
UNINITIALIZED_VALUE
), and then once it’s initialized, it will cast to
T
(https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/src/kotlin/util/Lazy.kt#L76-L85). For the sake of simplicity, I used boolean value.