Search code examples
kotlinsyntaxkotlin-lateinit

Is this correct "lateinit var text : String?"?


I need to know if this line code is right, my teacher told me it's correct, but I disagree 'cause "lateinit" can't be with a variable that could be null or not. Line code:

    lateinit var text : String?

Code:

    val cadena = null
    lateinit var text : String?
    text = null
    text = cadena ?: "Hola"
    text?.let { println(text) }

Solution

  • You are correct and your teacher is wrong. Proof: lateinit var text : String? results in compilation error with Kotlin 1.3.50:

    'lateinit' modifier is not allowed on properties of nullable types

    How any teacher can possibly claim such code is correct is beyond me...