Accessing anonymous object properties in Kotlin only works when the object is private

37 Views Asked by At

The following results in "Unresolved reference" compiler error for colour

fun main() {
    Tomato().printInfo()
}


class Tomato(){
    
    val features = object {
        val colour = "red"
    }
    
    fun printInfo(){
        println("The tomato is: ${features.colour}")
    } 
}

However, if I make the features object private ( ie. private val features = object ...) the code works. What is the reason behind this?

(Kotlin 1.8.21)

0

There are 0 best solutions below