Reputation: 1561
According to Kotlin documentation:
Members of the companion object can be called by using simply the class name as the qualifier.
Why does it not seem to work here?
class Foo {
companion object {
enum class Type { A, B, C }
}
}
class Bar {
val typeA = Foo.Companion.Type.A // works
val typeB = Foo.Type.B // error: "Unresolved reference: Type"
}
Upvotes: 6
Views: 3159
Reputation: 148189
Comparing the two qualified type names, Foo.Type.A
and Foo.Companion.Type.A
, the former would rather mean a type declared directly inside the Foo
's scope.
The latter form, therefore, is used to disambiguate types declared inside a type from ones declared inside its nested types and object declarations (including the companion object
).
class Foo {
class Bar // Foo.Bar
companion object {
class Bar // Foo.Companion.Bar
}
object Baz {
class Bar // Foo.Baz.Bar
}
}
As Pawel noted, nested types and object declarations are not members and have different resolution rules than those of functions and properties.
Upvotes: 5