Skip to content

Instantly share code, notes, and snippets.

@Teagan42
Last active December 17, 2018 21:15
Show Gist options
  • Save Teagan42/e4938bd9b880011cee68b6fb9baaab70 to your computer and use it in GitHub Desktop.
Save Teagan42/e4938bd9b880011cee68b6fb9baaab70 to your computer and use it in GitHub Desktop.
isInitialized is always false when overriding a lateinit var
open class ParentClass {
open lateinit var property: String
val isInitialized: Boolean
get() = this::property.isInitialized
val isLateInit: Boolean
get() = this::property.isLateinit
}
class ChildClass(
override var property: String
) : ParentClass()
class AnotherChildClass : ParentClass() {
override lateinit var property: String
}
fun main(args: Array<String>) {
val parent = ParentClass()
val child = ChildClass("Foo")
val anotherChild = AnotherChildClass()
println("Parent initialized: ${parent.isInitialized}")
println("Parent isLateInit: ${parent.isLateInit}")
println("Child initialized: ${child.isInitialized}")
println("Child isLateInit: ${child.isLateInit}")
println("Another Child initialized: ${anotherChild.isInitialized}")
println("Another Child isLateInit: ${anotherChild.isLateInit}")
parent.property = "Bar"
child.property = "Bar"
anotherChild.property = "Bar"
println("Parent initialized: ${parent.isInitialized}")
println("Parent isLateInit: ${parent.isLateInit}")
println("Child initialized: ${child.isInitialized}")
println("Child isLateInit: ${child.isLateInit}")
println("Another Child initialized: ${anotherChild.isInitialized}")
println("Another Child isLateInit: ${anotherChild.isLateInit}")
}
/*
Parent initialized: false
Parent isLateInit: true
Child initialized: false
Child isLateInit: true
Another Child initialized: false
Another Child isLateInit: true
Parent initialized: true
Parent isLateInit: true
Child initialized: false
Child isLateInit: true
Another Child initialized: false
Another Child isLateInit: true
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment