From de25e69db9811b4645f203ed3d9ed6ddb121d6de Mon Sep 17 00:00:00 2001 From: Mark Ipatov Date: Thu, 26 Sep 2024 09:48:42 +0200 Subject: [PATCH] KT-58965: Moved parts --- docs/src/md/kotlin.core/declarations.md | 80 ------------------------- docs/src/md/kotlin.core/inheritance.md | 80 +++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 80 deletions(-) diff --git a/docs/src/md/kotlin.core/declarations.md b/docs/src/md/kotlin.core/declarations.md index 46a0d7770..85346850d 100644 --- a/docs/src/md/kotlin.core/declarations.md +++ b/docs/src/md/kotlin.core/declarations.md @@ -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`. diff --git a/docs/src/md/kotlin.core/inheritance.md b/docs/src/md/kotlin.core/inheritance.md index ae71eee97..b7eaf7bab 100644 --- a/docs/src/md/kotlin.core/inheritance.md +++ b/docs/src/md/kotlin.core/inheritance.md @@ -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: @@ -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. +