Last active
December 17, 2018 21:15
-
-
Save Teagan42/e4938bd9b880011cee68b6fb9baaab70 to your computer and use it in GitHub Desktop.
isInitialized is always false when overriding a lateinit var
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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