Why can this Kotlin object inherit from itself?

I am trying to interact with TeamCity using Kotlin.

When you convert a project to Kotlin (from .xml), you will have a Project.kt file in which you can install all your configurations.

Without editing, my looks like this:

object Project : Project(/* Some Function Literal with Receiver */)

Does this look like circular inheritance? Is there an import that I omit, but of course that wouldn't make that much difference? Can the name be interpreted differently depending on where it appears in the file?

My mind interprets the signature of an object as follows:

  • object = object declaration for singleton.
  • Project (first occurrence) = Name of the object.
  • : = inheritance marker.
  • Project (second occurrence) = base class for inheritance.
  • () = constructor call to the base class.

Is this circular inheritance or did I miss something important about Kotlin? I looked here and here and can't seem to find my answer.

+4
source share
1 answer

Assuming cool classes other.Projectand my.Project, you have two different classes defined with the same name in different packages.

Since it is my.Projectdefined as Singleton, you cannot extend it, but the actual base class other.Project. Kotlin is smart enough to distinguish. If you try to do the same with the class declaration, you will get circular inheritance.

Singleton, Singleton. , .

+7

Source: https://habr.com/ru/post/1687444/


All Articles