Spot the bug in the changing host getter

from Null safety
Kotlin 2.4.10 intermediate 4 min 1 issue to find

The program should print the host length when one value is available. Find the unsafe assumption.

kotlin
class FlakyConfig {
    private var reads = 0
    val host: String?
        get() {
            reads += 1
            return if (reads == 1) "db.local" else null
        }
}

fun main() {
    val config = FlakyConfig()
    if (config.host != null) {
        println(config.host!!.length)
    }
}
Open in playground
Report an error