Consider the following Kotlin code:
object Domain {
val name = "local"
val location = object {
val city = "Pittsburgh"
val state = "Pennsylvania"
}
}
While this definition is valid and compiles, the following line fails:
val x = Domain.location.city // Error:(30, 27) Kotlin: Unresolved reference: city
However, if we rewrite the above definition as follows:
object City {
val city = "Pittsburgh"
val state = "Pennsylvania"
}
object Domain {
val name = "local"
val location = City
}
val x = Domain.location.city // works fine
My question: is this really correct behavior according to the language spec? This doesn't seem sensible or consistent. It makes it inconvenient to declare complex singleton object declarations without breaking each child object out into a top-level declaration. It seems like the compiler is creating anonymous types when this syntax is used, however, type of the assigned value is always object
when the definition is in a nested context. This almost seems like a type inference bug in the compiler. What am I missing?