Skip to content

Commit de25e69

Browse files
KT-58965: Moved parts
1 parent 59240df commit de25e69

File tree

2 files changed

+80
-80
lines changed

2 files changed

+80
-80
lines changed

Diff for: docs/src/md/kotlin.core/declarations.md

-80
Original file line numberDiff line numberDiff line change
@@ -306,86 +306,6 @@ Inner classes cannot be declared in [object declarations][Object declaration], a
306306
> }
307307
> ```
308308
309-
##### Inheritance delegation
310-
311-
In a classifier (an object or a class) declaration $C$, any supertype $I$ inheritance may be *delegated to* an arbitrary value $v$ if:
312-
313-
- The supertype $I$ is an interface type;
314-
- $v$ has type $T$ such that $T <: I$.
315-
316-
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.
317-
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).
318-
319-
The particular means on how $v$ is stored inside the classifier object is platform-defined.
320-
321-
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.
322-
323-
> Example:
324-
>
325-
> ```kotlin
326-
> interface I {
327-
> fun foo(value: Int): Double
328-
> val bar: Long
329-
> }
330-
> interface J : I {
331-
> fun fee(): Int
332-
> }
333-
>
334-
> class C(delegatee: I): I by delegatee
335-
> ```
336-
>
337-
> is expanded to
338-
>
339-
> ```kotlin
340-
> interface I {
341-
> fun foo(value: Int): Double
342-
> val bar: Long
343-
> }
344-
> interface J : I {
345-
> fun fee(): Int
346-
> }
347-
>
348-
> class C(delegatee: I): I {
349-
> val I$delegate = delegate
350-
>
351-
> override fun foo(value: Int): Double = I$delegate.foo(value)
352-
> override val bar: Long
353-
> get() = I$delegate.bar
354-
> }
355-
> ```
356-
>
357-
> 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.
358-
> See [classifier initialization section][Classifier initialization] for details on the evaluation order of classifier initialization entities.
359-
>
360-
> For example (assuming interface `I` from the previous example is defined):
361-
>
362-
> ```kotlin
363-
> var mut = object: J {...}
364-
>
365-
> class D: I by mut // D delegates I to mutable property
366-
> ```
367-
>
368-
> is expanded to
369-
>
370-
> ```kotlin
371-
> var mut = object: J {...}
372-
> class D: I {
373-
> val I$delegate = mut // mut is accessed only once
374-
>
375-
> override fun foo(value: Int): Double = I$delegate.foo(value)
376-
> override val bar: Long
377-
> get() = I$delegate.bar
378-
> }
379-
> ```
380-
>
381-
> ```kotlin
382-
> mut = x1
383-
> val d1 = D() // d1 methods are delegated to x1
384-
> mut = x2
385-
> val d2 = D() // d2 methods are delegated to x2
386-
> // but d1 methods are still delegated to x1
387-
> ```
388-
389309
##### Abstract classes {#abstract-classes-declarations}
390310
391311
A [class declaration][Class declaration] can be marked `abstract`.

Diff for: docs/src/md/kotlin.core/inheritance.md

+80
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,85 @@ As Kotlin is a language with single inheritance (only one supertype can be a cla
8080

8181
TODO(Examples)
8282

83+
### Inheritance delegation
84+
85+
In a classifier (an object or a class) declaration $C$, any supertype $I$ inheritance may be *delegated to* an arbitrary value $v$ if:
86+
87+
- The supertype $I$ is an interface type;
88+
- $v$ has type $T$ such that $T <: I$.
89+
90+
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.
91+
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).
92+
93+
The particular means on how $v$ is stored inside the classifier object is platform-defined.
94+
95+
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.
96+
97+
> Example:
98+
>
99+
> ```kotlin
100+
> interface I {
101+
> fun foo(value: Int): Double
102+
> val bar: Long
103+
> }
104+
> interface J : I {
105+
> fun fee(): Int
106+
> }
107+
>
108+
> class C(delegatee: I): I by delegatee
109+
> ```
110+
>
111+
> is expanded to
112+
>
113+
> ```kotlin
114+
> interface I {
115+
> fun foo(value: Int): Double
116+
> val bar: Long
117+
> }
118+
> interface J : I {
119+
> fun fee(): Int
120+
> }
121+
>
122+
> class C(delegatee: I): I {
123+
> val I$delegate = delegate
124+
>
125+
> override fun foo(value: Int): Double = I$delegate.foo(value)
126+
> override val bar: Long
127+
> get() = I$delegate.bar
128+
> }
129+
> ```
130+
>
131+
> 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.
132+
> See [classifier initialization section][Classifier initialization] for details on the evaluation order of classifier initialization entities.
133+
>
134+
> For example (assuming interface `I` from the previous example is defined):
135+
>
136+
> ```kotlin
137+
> var mut = object: J {...}
138+
>
139+
> class D: I by mut // D delegates I to mutable property
140+
> ```
141+
>
142+
> is expanded to
143+
>
144+
> ```kotlin
145+
> var mut = object: J {...}
146+
> class D: I {
147+
> val I$delegate = mut // mut is accessed only once
148+
>
149+
> override fun foo(value: Int): Double = I$delegate.foo(value)
150+
> override val bar: Long
151+
> get() = I$delegate.bar
152+
> }
153+
> ```
154+
>
155+
> ```kotlin
156+
> mut = x1
157+
> val d1 = D() // d1 methods are delegated to x1
158+
> mut = x2
159+
> val d2 = D() // d2 methods are delegated to x2
160+
> // but d1 methods are still delegated to x1
161+
> ```
83162
### Overriding
84163
85164
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
145224
> 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.
146225
> In this case these two declarations follow the normal rules of [overloading][Overload resolution].
147226
> However, these declarations may still result in a compile-time error as a result of [conflicting overload][Conflicting overloads] detection.
227+

0 commit comments

Comments
 (0)