Skip to content

Commit 81202c8

Browse files
author
Roman Venediktov
committed
KEEP-0430: Added paragraph for typed delegate access
1 parent ad9b3d6 commit 81202c8

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

proposals/KEEP-0430-explicit-backing-fields.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,43 @@ For now, however, we would prefer to keep the question of this possibility open.
305305

306306
Existing Kotlin reflection functionality will behave in the way as if there were no explicit backing fields.
307307
However, additional functionality will be added to support explicit backing fields.
308+
309+
## Related proposals
310+
311+
Another feature similar to this one, which might be introduced in the future, is **typed delegate access**.
312+
The idea is to provide access to the instance of a property delegate within the private scope of the property itself.
313+
This could be achieved by introducing a typed subclass of `KPropertyN`, namely `KDelegatedPropertyN`, which specializes the type of the property’s delegate.
314+
315+
For example, for `KDelegatedProperty0`, it might look as follows:
316+
317+
```kotlin
318+
interface KDelegatedProperty0<out D, out V> : KProperty0<V> {
319+
override fun getDelegate(): D // covariant override, intrinsic
320+
}
321+
```
322+
323+
Using the terminology of this KEEP,
324+
we can say that a reference to a delegated property acts as an explicit backing field of type `KDelegatedProperty0` within the private scope,
325+
and as a regular `KProperty0` when accessed from outside:
326+
327+
```kotlin
328+
class FooBar {
329+
val prop by lazy { "string lazy" }
330+
331+
fun f() {
332+
::prop // KDelegatedProperty0<Lazy<String>, String>
333+
::prop.getDelegate() // Lazy<String>, typed and without reflection ceremony
334+
if (::prop.getDelegate().isInitialized()) {
335+
// ...
336+
}
337+
}
338+
}
339+
340+
fun external() {
341+
FooBar()::prop // KProperty0<String>
342+
}
343+
```
344+
345+
Although this feature is conceptually similar to the current one, a property reference is not a property itself,
346+
and therefore this idea cannot be implemented as part of the current KEEP.
347+
A separate KEEP will be provided for this feature, with more details.

0 commit comments

Comments
 (0)