Skip to content

Commit

Permalink
KT-58965: Moved parts
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkTheHopeful committed Sep 26, 2024
1 parent 59240df commit de25e69
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 80 deletions.
80 changes: 0 additions & 80 deletions docs/src/md/kotlin.core/declarations.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,86 +306,6 @@ Inner classes cannot be declared in [object declarations][Object declaration], a
> }
> ```
##### Inheritance delegation
In a classifier (an object or a class) declaration $C$, any supertype $I$ inheritance may be *delegated to* an arbitrary value $v$ if:
- The supertype $I$ is an interface type;
- $v$ has type $T$ such that $T <: I$.
The inheritance delegation uses a syntax similar to [property delegation][Delegated property declaration] using the `by` keyword, but is specified in the classifier declaration header and is a very different concept.
If inherited using delegation, each method $M$ of $I$ (whether they have a default implementation or not) is delegated to the corresponding method of $v$ as if it was overridden in $C$ with all the parameter values directly passed to the corresponding method in $v$, unless the body of $C$ itself has a suitable override of $M$ (see the [method overriding][Overriding] section).
The particular means on how $v$ is stored inside the classifier object is platform-defined.
Due to the [initialization order of a classifier object][Classifier initialization], the expression used to construct $v$ can not access any of the classifier object properties or methods excluding the parameters of the primary constructor.
> Example:
>
> ```kotlin
> interface I {
> fun foo(value: Int): Double
> val bar: Long
> }
> interface J : I {
> fun fee(): Int
> }
>
> class C(delegatee: I): I by delegatee
> ```
>
> is expanded to
>
> ```kotlin
> interface I {
> fun foo(value: Int): Double
> val bar: Long
> }
> interface J : I {
> fun fee(): Int
> }
>
> class C(delegatee: I): I {
> val I$delegate = delegate
>
> override fun foo(value: Int): Double = I$delegate.foo(value)
> override val bar: Long
> get() = I$delegate.bar
> }
> ```
>
> Please note that the expression used as delegate is accessed exactly once when creating the object, e.g. if the delegate expression contains a mutable property access, this mutable property is accessed once during object construction and its subsequent changes do not affect the delegated interface functions.
> See [classifier initialization section][Classifier initialization] for details on the evaluation order of classifier initialization entities.
>
> For example (assuming interface `I` from the previous example is defined):
>
> ```kotlin
> var mut = object: J {...}
>
> class D: I by mut // D delegates I to mutable property
> ```
>
> is expanded to
>
> ```kotlin
> var mut = object: J {...}
> class D: I {
> val I$delegate = mut // mut is accessed only once
>
> override fun foo(value: Int): Double = I$delegate.foo(value)
> override val bar: Long
> get() = I$delegate.bar
> }
> ```
>
> ```kotlin
> mut = x1
> val d1 = D() // d1 methods are delegated to x1
> mut = x2
> val d2 = D() // d2 methods are delegated to x2
> // but d1 methods are still delegated to x1
> ```
##### Abstract classes {#abstract-classes-declarations}
A [class declaration][Class declaration] can be marked `abstract`.
Expand Down
80 changes: 80 additions & 0 deletions docs/src/md/kotlin.core/inheritance.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,85 @@ As Kotlin is a language with single inheritance (only one supertype can be a cla

TODO(Examples)

### Inheritance delegation

In a classifier (an object or a class) declaration $C$, any supertype $I$ inheritance may be *delegated to* an arbitrary value $v$ if:

- The supertype $I$ is an interface type;
- $v$ has type $T$ such that $T <: I$.

The inheritance delegation uses a syntax similar to [property delegation][Delegated property declaration] using the `by` keyword, but is specified in the classifier declaration header and is a very different concept.
If inherited using delegation, each method $M$ of $I$ (whether they have a default implementation or not) is delegated to the corresponding method of $v$ as if it was overridden in $C$ with all the parameter values directly passed to the corresponding method in $v$, unless the body of $C$ itself has a suitable override of $M$ (see the [method overriding][Overriding] section).

The particular means on how $v$ is stored inside the classifier object is platform-defined.

Due to the [initialization order of a classifier object][Classifier initialization], the expression used to construct $v$ can not access any of the classifier object properties or methods excluding the parameters of the primary constructor.

> Example:
>
> ```kotlin
> interface I {
> fun foo(value: Int): Double
> val bar: Long
> }
> interface J : I {
> fun fee(): Int
> }
>
> class C(delegatee: I): I by delegatee
> ```
>
> is expanded to
>
> ```kotlin
> interface I {
> fun foo(value: Int): Double
> val bar: Long
> }
> interface J : I {
> fun fee(): Int
> }
>
> class C(delegatee: I): I {
> val I$delegate = delegate
>
> override fun foo(value: Int): Double = I$delegate.foo(value)
> override val bar: Long
> get() = I$delegate.bar
> }
> ```
>
> Please note that the expression used as delegate is accessed exactly once when creating the object, e.g. if the delegate expression contains a mutable property access, this mutable property is accessed once during object construction and its subsequent changes do not affect the delegated interface functions.
> See [classifier initialization section][Classifier initialization] for details on the evaluation order of classifier initialization entities.
>
> For example (assuming interface `I` from the previous example is defined):
>
> ```kotlin
> var mut = object: J {...}
>
> class D: I by mut // D delegates I to mutable property
> ```
>
> is expanded to
>
> ```kotlin
> var mut = object: J {...}
> class D: I {
> val I$delegate = mut // mut is accessed only once
>
> override fun foo(value: Int): Double = I$delegate.foo(value)
> override val bar: Long
> get() = I$delegate.bar
> }
> ```
>
> ```kotlin
> mut = x1
> val d1 = D() // d1 methods are delegated to x1
> mut = x2
> val d2 = D() // d2 methods are delegated to x2
> // but d1 methods are still delegated to x1
> ```
### Overriding
A callable declaration (that is, a [property][Property declaration] or [member function][Function declaration] declaration) inside a classifier declaration is said to be *overridable* if:
Expand Down Expand Up @@ -145,3 +224,4 @@ If the overriding declaration *does* have its visibility specified, it must not
> Note: if a declaration binds a new function to the same name as was introduced in the base class, but which does not subsume it, it is neither a compile-time error nor an overriding declaration.
> In this case these two declarations follow the normal rules of [overloading][Overload resolution].
> However, these declarations may still result in a compile-time error as a result of [conflicting overload][Conflicting overloads] detection.

0 comments on commit de25e69

Please sign in to comment.